array reorder
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
array reorder
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?
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
Hi Da_Elf,
Best
Klaus
are that the KEYS of your array or conent of any key?If i have an array that contains D,B,E,F,A,C
Best
Klaus
Re: array reorder
those are the contents. they would just be indexed as array[1] = A , array[2] = B etc
Re: array reorder
Assuming you actually have an array where tArr[1] is "D" etc etc thenOh 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 withbut I'm still not clear what you're trying to do
Code: Select all
combine tArr with comma
sort items of tArr
split tArr by comma
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
Last edited by SparkOut on Sun Jul 20, 2014 7:19 pm, edited 1 time in total.
Re: array reorder
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: array reorder
Maybe you could consider using the value as the key?Da_Elf wrote:those are the contents. they would just be indexed as array[1] = A , array[2] = B etc
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: array reorder
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
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.
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
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
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
-
- VIP Livecode Opensource Backer
- Posts: 10052
- Joined: Sat Apr 08, 2006 7:05 am
- Contact:
Re: array reorder
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.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
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: array reorder
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:
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:
Kind regards,
Mark
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
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
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
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode
Re: array reorder
thanks everyone. mark, that was perfect