[Gambas-user] Array.MoveTo - a proposal

Rolf-Werner Eilert eilert-sprachen at ...221...
Tue Oct 4 10:27:58 CEST 2011


Hi folks,

This must have been a rare thing for me, at least I feel I've never 
coded something like this for the last few years. Please remember I 
still use Gambas 2.

In an array (here: string[]) I need to move one part to another place, 
kinda sort does, but only once.

I let the thing search for the positions of the thing to move and the 
thing to move to, and then...? Is there a ready-made function for it? I 
didn't find any, so I did this:

1. Look for the source thing (poSource)
2. Look for the goal position (poGoal)

My definition was like MoveBehind or MoveAfter, so it would read

array.MoveBehind(poSource, poGoal)

whereas poGoal means "set it behind/after that position" i.e. "MoveTo 
poGoal + 1".

The tricky thing is that once you delete the source at its original 
position, the array counts one less. And if you want to move the thing 
to a position AFTER its original one, you have to be aware to count one 
less because otherwise you end up one jump too far.

Although in my special case I could ignore this, I want to make sure the 
routine is safe in any case. So I decided to make a copy first, letting 
the counting untouched. However, after inserting the copy to its new 
position, the function will have to re-find its earlier position, at 
least if you copy to a place BEFORE. Now the problem is there again.

So if there is no such function in Gambas 3, here is my proposal: 
Introduce one! :-) It could be named MoveTo, so the programmer can 
decide whether it should implement Before or After in a specific 
project. I guess it is easily done once you must already have a similar 
thing in the Sort algorithm. At least this would avoid a lot of Gambas code.

Hope you understand what I mean ;-)

Regards

Rolf




More information about the User mailing list