ordered Array

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

foosmaster
Posts: 30
Joined: Mon Apr 13, 2020 8:46 am

ordered Array

Post by foosmaster » Mon Apr 13, 2020 7:51 pm

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

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: ordered Array

Post by bogs » Mon Apr 13, 2020 8:00 pm

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.
Image

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

Re: ordered Array

Post by FourthWorld » Mon Apr 13, 2020 8:14 pm

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

foosmaster
Posts: 30
Joined: Mon Apr 13, 2020 8:46 am

Re: ordered Array

Post by foosmaster » Mon Apr 13, 2020 8:51 pm

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

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

Re: ordered Array

Post by dunbarx » Mon Apr 13, 2020 8:57 pm

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

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: ordered Array

Post by bogs » Mon Apr 13, 2020 9:13 pm

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.
Image

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

Re: ordered Array

Post by dunbarx » 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

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

Re: ordered Array

Post by FourthWorld » Mon Apr 13, 2020 9:45 pm

You don't even need to pad the numbers. Sorting numeric will handle that.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: ordered Array

Post by bogs » Mon Apr 13, 2020 9:55 pm

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.
Image

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: ordered Array

Post by bogs » Mon Apr 13, 2020 10:04 pm

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
Image

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

Re: ordered Array

Post by FourthWorld » Mon Apr 13, 2020 10:20 pm

You can also sort by date and/or time if the keys contain valid data for that, using the datetime option.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

foosmaster
Posts: 30
Joined: Mon Apr 13, 2020 8:46 am

Re: ordered Array

Post by foosmaster » Mon Apr 13, 2020 10:21 pm

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 ?

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

Re: ordered Array

Post by SparkOut » Mon Apr 13, 2020 10:27 pm

@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

foosmaster
Posts: 30
Joined: Mon Apr 13, 2020 8:46 am

Re: ordered Array

Post by foosmaster » Mon Apr 13, 2020 10:33 pm

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

bogs
Posts: 5480
Joined: Sat Feb 25, 2017 10:45 pm

Re: ordered Array

Post by bogs » Mon Apr 13, 2020 11:16 pm

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 :oops: 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.
Image

Post Reply