array reorder

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

array reorder

Post by Da_Elf » Sun Jul 20, 2014 4:30 pm

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?

Klaus
Posts: 14199
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: array reorder

Post by Klaus » Sun Jul 20, 2014 4:37 pm

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

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: array reorder

Post by Da_Elf » Sun Jul 20, 2014 4:39 pm

those are the contents. they would just be indexed as array[1] = A , array[2] = B etc

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: array reorder

Post by SparkOut » Sun Jul 20, 2014 7:07 pm

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
Last edited by SparkOut on Sun Jul 20, 2014 7:19 pm, edited 1 time in total.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: array reorder

Post by dunbarx » Sun Jul 20, 2014 7:14 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: array reorder

Post by FourthWorld » Sun Jul 20, 2014 7:48 pm

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?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: array reorder

Post by Da_Elf » Sun Jul 20, 2014 9:08 pm

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

SparkOut
Posts: 2947
Joined: Sun Sep 23, 2007 4:58 pm

Re: array reorder

Post by SparkOut » Sun Jul 20, 2014 9:32 pm

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.

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10333
Joined: Wed May 06, 2009 2:28 pm

Re: array reorder

Post by dunbarx » Sun Jul 20, 2014 9:39 pm

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

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: array reorder

Post by FourthWorld » Sun Jul 20, 2014 11:40 pm

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.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Re: array reorder

Post by Mark » Sun Jul 20, 2014 11:47 pm

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
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

Da_Elf
Posts: 311
Joined: Sun Apr 27, 2014 2:45 am

Re: array reorder

Post by Da_Elf » Mon Jul 21, 2014 5:18 pm

thanks everyone. mark, that was perfect

Post Reply