Page 1 of 3
ordered Array
Posted: Mon Apr 13, 2020 7:51 pm
by foosmaster
if I get this correctly - an array is really a hash/map/dictiionary of key/value
when I loop over the keys:
Code: Select all
repeat for each key tKey in tArray
I get it in random order, is there a way to get the keys in the order they're in the array ?
is an array ordered ? if not, is there another type that's ordered is guaranteed ?
I am adding this:
id1 => name1
id2 => name2
id3 => name3
the names are unique, so are the ids (of course)
I need to get the keys in the same order - hopefully without creating a loop through the names and then finding the id of the name I just found
Re: ordered Array
Posted: Mon Apr 13, 2020 8:00 pm
by bogs
Probably more than you will ever need to know about arrays in Lc...
http://livecode.byu.edu/arrays/introToArrays.php
http://livecode.byu.edu/arrays/arrays_inclass_ex.html
http://livecode.byu.edu/arrays/arrayExercise.php
A gratuitous link no where near the category of the above -
https://www.youtube.com/watch?v=E_kDfR7 ... dex=7&t=0s
You can either watch the whole painful thing, or more likely, use the chapter links to skip down to sorting.
Re: ordered Array
Posted: Mon Apr 13, 2020 8:14 pm
by FourthWorld
LiveCode Script supports associative arrays, and does not currently offer indexed arrays.
Associative arrays are name-value pairs, where the name can be any alphanumeric string up to 255 characters.
Even if the name (or "key") is only numeric characters, it's still a string.
Associative arrays do not have a sense of order internally, so if you need to work with their keys in order you can get the keys of the array and sort that list, using that to walk through the elements in your loop.
Re: ordered Array
Posted: Mon Apr 13, 2020 8:51 pm
by foosmaster
Hi,
Thanks for your help.
it makes sense that associative array don't have a guaranteed order - that's why I want to make sure that if I loop through the values, the order remains
(otherwise, I'll have to create another array with indexed that I'll maintain, and go through them - sorted)
couldn't find the answer to this question in the links bogs has sent:
is it guaranteed that looping through the values of the array will return the same order ?
Thanks again
Re: ordered Array
Posted: Mon Apr 13, 2020 8:57 pm
by dunbarx
Hi.
What everyone said. But to make sure you "get" it, all such processing, especially sorting, of your array is done "back" in LiveCode. You bring your data out, do stuff as required, and if you wish reconstitute the whole back into an array. The sorting will be lost when you do, but there is no real burden in dealing with your data in the two worlds. You only get to "see" the data "in the clear" anyway, that is, in an ordinary variable or container
Similar to a Datagrid, I always extract the entirety of the dataSet, process as needed, and the reload. Just my comfort zone.
Sorry for all the quotes.
Craig
Re: ordered Array
Posted: Mon Apr 13, 2020 9:13 pm
by bogs
foosmaster wrote: ↑Mon Apr 13, 2020 8:51 pm
couldn't find the answer to this question in the links bogs has sent:
is it guaranteed that looping through the values of the array will return the same order ?
If you need to return a specific order guaranteed, then I would construct the array with the key being a formatted number, i.e. let's say you had 10,000 keys (if you do, I may come over and slap you!), and you needed those to be in a specific order. The key names would run like this :
00001
00002
00003
00004
00005
The keys can be sorted, without requiring a loop structure, so (if you watched the video) you could do something like:
sort the keys of tArray ascending numeric
That would certainly force the array keys to come up in a specific order.
Re: ordered Array
Posted: Mon Apr 13, 2020 9:42 pm
by dunbarx
Bogs.
True, but we are still processing "in the clear". The OP wanted to know if it was possible to, say, sort the keys of an array *IN* the array.
Nope. You have to bring them out.
Craig
Re: ordered Array
Posted: Mon Apr 13, 2020 9:45 pm
by FourthWorld
You don't even need to pad the numbers. Sorting numeric will handle that.
Re: ordered Array
Posted: Mon Apr 13, 2020 9:55 pm
by bogs
That padding comes from a number of systems that don't always behave like I expect them too, where I get results like -
1
12
2
20.. etc.
Re: ordered Array
Posted: Mon Apr 13, 2020 10:04 pm
by bogs
dunbarx wrote: ↑Mon Apr 13, 2020 9:42 pm
Bogs.
True, but we are still processing "in the clear". The OP wanted to know if it was possible to, say, sort the keys of an array *IN* the array.
Nope. You have to bring them out.
Craig
Sorry Craig, I completely missed that one, but straight from the dictionary...
put "1,ben" into line 1 of tData
put "2,elanor" into line 2 of tData
put "3,ali" into line 3 of tData
sort lines of tData descending numeric by item 1 of each
Re: ordered Array
Posted: Mon Apr 13, 2020 10:20 pm
by FourthWorld
You can also sort by date and/or time if the keys contain valid data for that, using the datetime option.
Re: ordered Array
Posted: Mon Apr 13, 2020 10:21 pm
by foosmaster
Ok, it's really a map/hash/associative array, not an array, I need to make some work around, got it.
It's there a way to get the count of keys ?
Re: ordered Array
Posted: Mon Apr 13, 2020 10:27 pm
by SparkOut
@bogs, in that case, tData is not an array.
For an array, you cannot sort the vslues. You can sort the keys, but realistically you are putting the keys into another container where they are sorted. Or think of it that "the keys" IS another container, containing the keys of an array.
"put the number of lines of the keys of myArray" will tell you how many there are
Re: ordered Array
Posted: Mon Apr 13, 2020 10:33 pm
by foosmaster
Awesome, thanks.
Still... I wonder his such a useful programming tool (array vector, list, call it what you will) is missing ...
Even php associative array is an ordered one, first language o see that doesn't have one
Yet again ... thanks a lot for your help
Re: ordered Array
Posted: Mon Apr 13, 2020 11:16 pm
by bogs
Yep, they are sure associative only, but they are very flexible. Ordering one takes one line of code to put the keys into a variable, and one line to order the variable itself. When your pulling key names, just pull from the variable.
Multi-dimensional arrays aren't much more work, so overall it is kind of a wash.
SparkOut wrote: ↑Mon Apr 13, 2020 10:27 pm
@bogs, in that case, tData is not an array.
My bad, I was pre-occupied with a few other things at the time I posted

However, it does give foosmaster another way to work without even using an array.
Since you can set the itemDelimiter to close to anything, you can treat variables like the one I posted just like an array, if you wanted. More to your questions point as it were, let us assume you had data you wanted to enter into an array that has to remain in the order it is entered, and it could not be easily sorted, like a list where the keys were:
purple
orange
apple
12
MyCar
If you had a list like that, and wanted to make sure that the order remained consistent forever as it is entered, you could simply put it into a variable, separating it and its value with almost anything (but don't use a comma, for the love of god!!) and set the itemDelim to that. For instance -
purple numToChar(30) the color of plums
orange numToChar(30) sunset at dawn
apple numToChar(30) a day
12 numToChar(30) miles to go
MyCar numToChar(30) is red
Actual code to illustrate:
Code: Select all
# to actually put this together, I would opt for a loop, but for this list
## I'll do it manually....
put "purple" & numToChar(30)& "the color of plums" into line 1 of tData
put "orange" & numToChar(30)& "sunset at dawn" into line 2 of tData
put "apple" & numToChar(30) & "a day" into line 3 of tData
put "12" & numToChar(30) & "miles to go" into line 4 of tData
put "MyCar" & numToChar(30) & "is red" into line 5 of tData
set the itemDelimiter to numToChar(30)
put tData & cr & cr & Item 1 of line 1 of tData & cr & item 1 of line 2 of tData & cr & item 1 of line 3 of tData & cr & item 1 of line 4 of tData & cr & "...etc"
Which outputs this -
raw variable -
purplethe color of plums
orangesunset at dawn
applea day
12miles to go
MyCaris red
putting the 1st item of each line...
purple
orange
apple
12
...etc
I'm sure you get the idea.