## Trying to understand arrays

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: Klaus, FourthWorld, heatherlaine, kevinmiller

redfield
Posts: 54
Joined: Thu Apr 04, 2019 1:41 pm

### Trying to understand arrays

Hi everybody,
I'm new to Livecode, in fact to programming. Have just a little bit experience with Python. I am now trying to understand arrays and how to handle them. Sorry for surely stupid beginner questions .

If I have two arrays (fruit and vegi) and put them into a third array (food), does this mean that food is then a two dimensional array?

Code: Select all

put banana into fruit[1]
put orange into fruit[2]

put broccoli into vegi[1]
put turnip into vegi[2]

put fruit into food[1]
put vegi into food[2]
And how can I get the output "orange" from array food? Thanks!

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

### Re: Trying to understand arrays

Heya redfield, welcome to the forums!

The best slice of text I've ever seen about arrays is this one at BYU.

If you go through the whole thing (recommended) then you should have all the answers you need. It covers -
Using Arrays
• Introduction to Arrays in LiveCode
Exercise: Working with Arrays
Exercise: Simple Dictionary Array
Just a note, arrays aren't 'newbie' or 'beginner' level topics, they are usually considered somewhat advanced. However, if you work your way through it, it shouldn't be too complex to understand.

Thierry
VIP Livecode Opensource Backer
Posts: 664
Joined: Wed Nov 22, 2006 3:42 pm
Location: France
Contact:

### Re: Trying to understand arrays

And how can I get the output "orange" from array food? Thanks!
try this:

Code: Select all

put food[1][2]
and then listen to bogs advice...

Good luck,

Thierry
Thierry Douez - https://sunny-tdz.com
Pourquoi tant de notes lorsqu'il suffit de jouer les plus belles... [Barbara]

FourthWorld
VIP Livecode Opensource Backer
Posts: 7043
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: Trying to understand arrays

You could retrieve "orange" in two steps, reflecting the two steps you used to assign it:

Code: Select all

put food[1] into fruit
put fruit[2] into tMyVarThatNowContainsOrange
But you may find using the descriptive label "fruit" more useful than assigning an arbitrary integer as the string used for that element name:

Code: Select all

put food["fruit"] into fruit
put fruit[2] into tMyVarThatContainsOrange
You can also either get or put that value in one step, using a set of brackets for each level in the nested array.

To put:

Code: Select all

put "orange" into food["fruit"][2]
To get:

Code: Select all

put food["fruit"][2] into tMyVarThatContainsOrange
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com

dunbarx
VIP Livecode Opensource Backer
Posts: 6266
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Trying to understand arrays

Here is one of my favorite gadgets to play with arrays. It counts things, like the occurrences of words in a body of text.

On a new card, make a button and a field. In the field, put something like:
cat dog cat hippo mouse mouseUp donkey ibis cat ibis ibis ibis dog monkey
In the button script:

Code: Select all

on mouseUp
get fld 1
--breakpoint  -- enable here when ready
repeat for each word tWord in it
end repeat

combine animalCount by return and comma
end mouseUp
If you step through the code by enabling the breakpoint, you can see the development of the array variable "animalCount". Remember to "open" the contents of the array in the debugger while stepping through.

This is also useful in knowing that the debugger is an excellent place to be able to see an array variable without having to change it into an ordinary variable, using the "combine" command above. An array is normally not visible in the real world.

Craig Newman

EDIT: Moved breakpoint to the correct place in the handler.

dunbarx
VIP Livecode Opensource Backer
Posts: 6266
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Trying to understand arrays

RedField.
Sorry for surely stupid beginner questions
LiveCode is never having to say you are sorry.

Craig

FourthWorld
VIP Livecode Opensource Backer
Posts: 7043
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

### Re: Trying to understand arrays

dunbarx wrote:
Thu Apr 04, 2019 5:52 pm
LiveCode is never having to say you are sorry.
I want that on a t-shirt in time for the conference next month.
Community volunteer LiveCode Community Liaison

LiveCode development, training, and consulting services: Fourth World Systems: http://FourthWorld.com

dunbarx
VIP Livecode Opensource Backer
Posts: 6266
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

### Re: Trying to understand arrays

Richard.

It is from the movie "Love Story", mangled, as I am wont to do. You may know that. Young'uns may not.

Craig

ValiantCuriosity
Posts: 128
Joined: Thu Jun 30, 2016 3:08 am

### Re: Trying to understand arrays

Hi @Redfield,

As one lC newbie to another, arrays and multidimensional arrays can be an adventure for a new user. Bogs was not steering you wrong when he said,
The best slice of text I've ever seen about arrays is this one at BYU.
Adding my very limited noob voice to those experts who have already answered, I'd recommend starting at the very beginning and going from there. Be sure that you understand how variables work before dealing with arrays. That will make it so much easier. If you don't understand the basics of LC, jumping right into arrays, might become overwhelming.

Do you already understand this code:

Code: Select all

global gName
on mouseUp
put it into gName
end mouseUp
Note: The above code was shamelessly quoted from the wonderful site listed below. This is another great place to experience LC in steps to learning.

Good Luck,
-Rachel
May I never be cured of my curiosity!

redfield
Posts: 54
Joined: Thu Apr 04, 2019 1:41 pm

### Re: Trying to understand arrays

Wow thanks everybody for the warm welcome and all the answers!
I will work my way through now, but I can already say that I tested the suggested code for displaying "orange" and it's working just fine. The BYU source indeed seems to be a great explanation on arrays, have to read it again though. Thanks so far (unfortunately so little time for this fun stuff ).

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

### Re: Trying to understand arrays

If the BYU course makes sense to you (it doesn't suite everyone), then by all means start at the beginning here, and work your way through when you have time.

There are a ton of ways to learn Lc that work for different people, Cyril's pages that Rachel mentioned, Jacque's The LiveCode Scripting Conferences, or even the old MetaCard reference system I separated out from the open source IDE, which I personally found invaluable.