[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

List.Swap - or like in Python


Just a question or an idea what could be useful.

When I want to sort an arry for instance, I will frequently have to swap contents of two different array cells like

array[x] = array[x+1]

and back. As the contents of array[x] is lost, I have to temporarily store one of the values like

sKeep = array[x]
array[x] = array[x+1]
array[x+1] = sKeep

The other day I stumbled over this one in Python: you can swap variable contents by just setting several variable names with commas on each side of the "=". If we translate it to Gambas, it would come out to

array[x], array[x+1] = array[x+1], array[x]

or more simple:

a, b = b, a

just swaps the contents of variables a and b.

There could be a function array.Swap(x, x+1) for arrays. But it would be only useful for arrays. Python's idea is more versatile as it covers single variables as well. You can mix arrays with variables and even set in expressions (like array[x], b = b, array[x]+3).

Could you imagine introducing such a thing for Gambas?

Regards
Rolf


Follow-Ups:
Re: List.Swap - or like in PythonBB <adamnt42@xxxxxxxxx>
Re: List.Swap - or like in PythonBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>