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

Re: List.Swap - or like in Python


Le lun. 11 mars 2024 à 12:05, Rolf-Werner Eilert <rwe-sse@xxxxxxxxxx> a
écrit :

> Am 11.03.24 um 11:08 schrieb Benoît Minisini:
> > Le 11/03/2024 à 09:44, Rolf-Werner Eilert a écrit :
> >> 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
> >>
> >>
> >
> > You can do in Gambas:
> >
> > Swap array[x], array[x + 1]
> >
> > But a dedicated array function array.Swap(x, x + 1) would be faster.
>

So you can add this to your todo list 😁



>
> > Regards,
> >
>
> Oh - my goodness, there it is, and I haven't known about it :)
>
> Thanks so much!
>
> Regards
> Rolf
>
>
>

References:
List.Swap - or like in PythonRolf-Werner Eilert <rwe-sse@xxxxxxxxxx>
Re: List.Swap - or like in PythonBenoît Minisini <benoit.minisini@xxxxxxxxxxxxxxxx>
Re: List.Swap - or like in PythonRolf-Werner Eilert <rwe-sse@xxxxxxxxxx>