Page 1 of 1

array reorder

Posted: Sun Jul 20, 2014 4:30 pm
by Da_Elf
Ive been killing my head on this one and im hoping there is a simple answer im missing
If i have an array that contains D,B,E,F,A,C
and i want to change it to D,E,F,B,A,C
this is done by moving B to where A is and reorder the array so that A and C are pushed to after B and everything is listed as the 2nd array
is this something simple or do i have to do it with 200 lines of code?

Re: array reorder

Posted: Sun Jul 20, 2014 4:37 pm
by Klaus
Hi Da_Elf,
If i have an array that contains D,B,E,F,A,C
are that the KEYS of your array or conent of any key?


Best

Klaus

Re: array reorder

Posted: Sun Jul 20, 2014 4:39 pm
by Da_Elf
those are the contents. they would just be indexed as array[1] = A , array[2] = B etc

Re: array reorder

Posted: Sun Jul 20, 2014 7:07 pm
by SparkOut
Assuming you actually have an array where tArr[1] is "D" etc etc then

Code: Select all

   combine tArr with comma
   sort items of tArr
   split tArr by comma
Oh wait, you don't want to sort the contents alphabetically...
What is it you actually have that you're trying to do? There's probably a simpler way to understand it.
For information, arrays in LiveCode are "hash tables" and the order of the keys has no bearing on the contents or vice versa. You probably don't need to assign tArr[1] to be "D", as long as you have a valid reference to which elements you want to look up.
I believe you could do what you want with

Code: Select all

   combine tArr with comma
   put item 2 of tArr into tTemp
   delete item 2 of tArr
   put comma & tTemp after item 3 of tArr
   split tArr by comma
but I'm still not clear what you're trying to do

Re: array reorder

Posted: Sun Jul 20, 2014 7:14 pm
by dunbarx
Hi.

You will have to get the array into an ordinary variable, using the "combine" command, and then process.

Since the re-ordering seems arbitrary, you will need either rules or a look-up table to do the work. 200 lines? Probably not, but without any algorithmic methodolgy, the length of the handler will depend on the length of your data.

So how is the re-ordering determined? A little analysis is worth a whole lot of coding.

Craig Newman

Re: array reorder

Posted: Sun Jul 20, 2014 7:48 pm
by FourthWorld
Da_Elf wrote:those are the contents. they would just be indexed as array[1] = A , array[2] = B etc
Maybe you could consider using the value as the key?
Does the collection need to be represented as an array at all?

Re: array reorder

Posted: Sun Jul 20, 2014 9:08 pm
by Da_Elf
Im trying to move a valuse "B" which is at index 2 on the original array to place it at index5 where "A" is. so basically A will be pushed over to make way for B and then the whole thing will tidy up to close the gap where B came from

Re: array reorder

Posted: Sun Jul 20, 2014 9:32 pm
by SparkOut
But why? what difference does it make? Hash tables are not the same as a contiguous array of indexed elements. What data structure do you have?
The second of my little code snippets above will do what you asked, but it's not clear what benefit you will get from that.

Re: array reorder

Posted: Sun Jul 20, 2014 9:39 pm
by dunbarx
What Sparkout means is that arrays in LC are purely associative, and their internal order is irrelevant. It is why the underlying data has to be sorted in an ordinary variable, if that is what is desired. In other words, there is a one-to-one relationship between the keys and elements of an array, but no internal order is guaranteed, whether or not the array was created with a certain level of order, for example, if it was made with an indexed repeat loop.

I have to ask again. Are there rules of any kind that determine how the interchanges will be made? Or must it be derived from a look-up table?

Craig

Re: array reorder

Posted: Sun Jul 20, 2014 11:40 pm
by FourthWorld
Da_Elf wrote:A will be pushed over to make way for B and then the whole thing will tidy up to close the gap where B came from
There is no "pushing over" of data in an associative array. Arrays are collections of pointers, conveniently accessed from a single variable name but actually residing in disparate locations. What happens in one array slot has no effect on the others.

I can't help but wonder if a simple delimited list may be better suited for what you want to do than attempting to use an array for this.

Re: array reorder

Posted: Sun Jul 20, 2014 11:47 pm
by Mark
Hi,

It seems you don't want an array but a list. If your original list is D,B,E,F,A,C, you can use the item numbers is ID numbers or "keys". Obviously, the item numbers are 1, 2, 3, 4, 5, 6. To move item 2 to item 4, use the following syntax:

Code: Select all

put "D,B,E,F,A,C" into myList
put comma & item 2 of myList after item 4 of myList
delete item 2 of myList
The item numbers are still 1 to 6, but the order of the items in the list have changed. If you move an item backwards, you need to change the item number when deleting it:

Code: Select all

put "D,E,F,B,A,C" into myList
put comma & item 4 of myList after item 1 of myList
delete item 5 of myList // 5 instead of 4
Kind regards,

Mark

Re: array reorder

Posted: Mon Jul 21, 2014 5:18 pm
by Da_Elf
thanks everyone. mark, that was perfect