what does myarray ["this stuff"] stands for ?

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

liveme
Posts: 240
Joined: Thu Aug 27, 2015 5:22 pm

Re: what does myarray ["this stuff"] stands for ?

Post by liveme » Thu Feb 25, 2021 7:54 pm

Thanks Klaus,
yesss, this looks more like an info source that I prefer..
cool ! :idea:

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

Re: what does myarray ["this stuff"] stands for ?

Post by bogs » Thu Feb 25, 2021 7:55 pm

Well no, I havent look into links that do not refer to some "LC website" lessons or tuto yet :
1 thing is ..if its not an LC made syntax, its likely to add to my confusion of how an LC code can be written like.
2nd I 've been trying to focus on the PDF dictionaries and guide but one is really very technical and doesnt offer simple complete code samples
and the other sometimes skip that part that one wanted to see how it is written.
1. I made the video, it is entirely written in Lc hee hee.

2. I highly commend you on focusing on the pdf documentation, it has (I think) improved quite a bit, but as you'll find out it can still be somewhat opaque. Certainly if you never look at my video, I would suggest the link glenn9 brought up, which is a pretty complete course in Lc, written by someone really REALLY knowledgeable about Lc, which goes from beginner to advanced concepts.

I see what Klaus posted, certainly worth going through as well.
Image

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

Re: what does myarray ["this stuff"] stands for ?

Post by Klaus » Thu Feb 25, 2021 7:55 pm

You're welcome!

The stacks are a bit older, so widgets do not get explained.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: what does myarray ["this stuff"] stands for ?

Post by jacque » Thu Feb 25, 2021 7:57 pm

indeed I first learned about array starting coding practice with Dart.
which uses LIST SET and MAP for 3 "types" of arrays...
I don't know Dart but this example sounds more like lists, not arrays.

It takes some time to understand array structures but once you do they are very versatile. One teaching example is to think of arrays as an egg carton. It has 12 or more cups. Each cup can contain any number of items. Suppose I want to know what is the second thing in cup 3. I would need to specify the name of the carton since I may have more than one. Then I specify the cup and then the number of the item :

Code: Select all

EggArray[3][2] -> 3rd cup, 2nd item
It is often easier to name the cups and items so you don't need to work with just numbers. Let's say every cup has some kind of fruit in it and I want to know what it is. I ask :

Code: Select all

EggArray[3]["fruit"] -> "strawberry" 
The cups can also have names. The name of the third cup is "food" and it has several types of food in it :

Code: Select all

EggArray["food"]["fruit"] -> strawberry 
EggArray["food"]["nut"] -> almond 
When using arrays you are basically constructing a path that points to the thing you want to know -- "what is the fruit in the third cup of the egg array?"

If you ask for a literal name like "fruit" above then it needs to be quoted like any other literal value. If instead the name is in a variable then of course you would not use quotes:

Code: Select all

put "fruit" into tKey
get EggArray["food"][tKey] 
(I wrote this while other posts appeared but I'll post it anyway.)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: what does myarray ["this stuff"] stands for ?

Post by elanorb » Thu Feb 25, 2021 8:01 pm

Hi

Just chiming in with another resource. If you have been looking at the PDF User Guide I am sure you have found the Array Variables section but if not I think this is quite a good explanation, this is just in addition to all the other great resources that have already been shared.

--------------------
A variable can hold more than a single value. A variable that holds more than one value is called an array, and each of the values it holds is called an element. Each element has its own name (called the element's key).

If you think of a variable as a box with a name, you can think of an array as a box with compartments inside it. Each compartment is an element, and each compartment has a name, or key, of its own.

You specify an element of an array variable by using the variable name along with the element's key. You enclose the key in square brackets. The key may be a name, number or variable. Here's an example that shows how to put data into one element of an array:

Code: Select all

put "ABC" into myVariable["myKeyName"]
Note: If you use a key that's not a number or variable, you should enclose the key's name in double quotes whenever you refer to it. This prevents problems in case there is a variable or reserved word with the same name.
--------------------

Elanor
Elanor Buchanan
Software Developer
LiveCode

elanorb
Livecode Staff Member
Livecode Staff Member
Posts: 516
Joined: Fri Feb 24, 2006 9:45 am

Re: what does myarray ["this stuff"] stands for ?

Post by elanorb » Thu Feb 25, 2021 8:02 pm

Like Jacque other posts appeared while I was writing mine!
Elanor Buchanan
Software Developer
LiveCode

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

Re: what does myarray ["this stuff"] stands for ?

Post by bogs » Thu Feb 25, 2021 8:05 pm

Thats ok Elanor, we love seeing you here :)
Image

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: what does myarray ["this stuff"] stands for ?

Post by jacque » Thu Feb 25, 2021 9:40 pm

bogs wrote:
Thu Feb 25, 2021 8:05 pm
Thats ok Elanor, we love seeing you here :)
I love seeing Elanor anywhere. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: what does myarray ["this stuff"] stands for ?

Post by dunbarx » Thu Feb 25, 2021 9:46 pm

All good stuff, but it still takes practice. Or at least I needed to.

Here is another way to see what is going on. Put this in a button script:

Code: Select all

on mouseUp
   put "duck" into myArray[bird][swimmer]
   put "dove" into myArray[bird][flyer]
   put "chicken" into myArray[bird][delicious]
   breakpoint
   
   put myArray into newArray ---1
   put myArray[bird] into yetAnotherArray  --2
   
   put myArray[swimmer] into x ---3
end mouseUp
Number "1 shows that you can move arrays intact into another variable. #2 shows that you can still do that if even you specify an "early" key that holds throughout the array variable itself.

#3 shows that the structure of the array/key hierarchy cannot be manipulated. Early on, for me, I could not wrap my head around why I could not extract "duck" from that (#3) line of code. But you cannot because you are querying data in an element from a key that does not exist. Those compartments that everyone spoke about are strictly ordered in terms of earlier elements being keys for later ones. The array does not "know" that you are asking for something that, though it exists in one structure, does not exist in another.

Craig

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

Re: what does myarray ["this stuff"] stands for ?

Post by bogs » Thu Feb 25, 2021 9:50 pm

jacque wrote:
Thu Feb 25, 2021 9:40 pm
bogs wrote:
Thu Feb 25, 2021 8:05 pm
Thats ok Elanor, we love seeing you here :)
I love seeing Elanor anywhere. :)
I'm sure she is sick of seeing me in her email box though hee hee

Back on the topic (such as it is), what Craig said, a bit of practice with it would set you straight in no time. If you decide you want to put your array into a field, there are a few ways to do it.

One uses 'combine with return and tab' , which certainly works.

Another way you could do it is to loop through the array, putting the key & tab & the key value. Either road works, though.
Image

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

Re: what does myarray ["this stuff"] stands for ?

Post by Klaus » Thu Feb 25, 2021 10:06 pm

I HIGHLY recommend to QUOTE non-numeric key names!

Code: Select all

put "duck" into myArray["bird"]["swimmer"]
The engine is less forgiving with "sloppy syntax" in every new version as I experienced during the last 21 years!

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am

Re: what does myarray ["this stuff"] stands for ?

Post by Davidv » Thu Feb 25, 2021 10:52 pm

I agree with Jacque that some of the confusion here may arise from the difference between lists and arrays. The Dart sets seem more akin to lists than to arrays, in terms of prior experience. As a learning path, mastering at least the basics of lists may pay before delving further into keyed arrays. If lists are already well understood then the differences become clearer.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7391
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: what does myarray ["this stuff"] stands for ?

Post by jacque » Thu Feb 25, 2021 11:59 pm

The OP is probably tired of this by now, but...

Lists are strings delimited by almost anything. That is, a designated character or characters separate the parts. Using Craig's example, I could put any of thise into a variable named "tBirdList":

Code: Select all

Return delimited:
duck,bird,swimmer
dove,bird,flyer
chicken,bird,delicious

Pipe delimited:
duck,bird,swimmer|dove,bird,flyer|chicken,bird,delicious

Slash delimited:
duck,bird,swimmer/dove,bird,flyer/chicken,bird,delicious

Multi-character delimited:
duck,bird,swimmer_#_dove,bird,flyer_#_chicken,bird,delicious
To extract something from a list, we use text chunking. If I want to know about ducks, I'd ask for "line 1 of tBirdList" in the first example. If I want to know what kind of bird, I'd get "item 3 of line 1 of tBirdList". To find out what bird swims takes more parsing; you need to run a repeat loop and look at all the entries, and extract the ones that are swimmers.

Arrays are groups of related data designated by their "keys", that is, the name of each group. Each part of the group is an "element". Here is the same example as an array:
Screen Shot 2021-02-25 at 4.37.57 PM.png
There is only one key in myArray, "bird". The bird element is also an array, with three keys: delicious, flyer, and swimmer. The value of those keys are chicken, dove, and duck respectively. To find out what kind of bird is a swimmer, use array notation:

Code: Select all

myArray["bird"]["swimmer"]
Arrays are far faster than parsing lists. You can extract information from an array with thousands of entries in just milliseconds.

Let us know when you get tired of this. :)
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

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

Re: what does myarray ["this stuff"] stands for ?

Post by bogs » Fri Feb 26, 2021 12:15 am

The OP might get tired of it, but I'm sure finding it interesting :twisted:
Image

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

Re: what does myarray ["this stuff"] stands for ?

Post by dunbarx » Fri Feb 26, 2021 12:45 am

I HIGHLY recommend to QUOTE non-numeric key names!
I always do, Klaus, but I wrote this while in the shower, and was just a little lazy.

Craig

Post Reply