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

Post Reply
redfield
Posts: 95
Joined: Thu Apr 04, 2019 1:41 pm

Trying to understand arrays

Post by redfield » Thu Apr 04, 2019 1:59 pm

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

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: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Trying to understand arrays

Post by bogs » Thu Apr 04, 2019 2:47 pm

Heya redfield, welcome to the forums! :D

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

Thierry
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 875
Joined: Wed Nov 22, 2006 3:42 pm

Re: Trying to understand arrays

Post by Thierry » Thu Apr 04, 2019 2:54 pm

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... :roll:

Good luck,

Thierry
!
SUNNY-TDZ.COM doesn't belong to me since 2021.
To contact me, use the Private messages. Merci.
!

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

Re: Trying to understand arrays

Post by FourthWorld » Thu Apr 04, 2019 3:00 pm

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

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

Re: Trying to understand arrays

Post by dunbarx » Thu Apr 04, 2019 5:02 pm

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
      add 1 to animalCount[tWord]
   end repeat
    
     combine animalCount by return and comma
   answer animalCount
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. :oops:

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

Re: Trying to understand arrays

Post by dunbarx » Thu Apr 04, 2019 5:52 pm

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

Craig

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

Re: Trying to understand arrays

Post by FourthWorld » Fri Apr 05, 2019 1:08 am

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

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

Re: Trying to understand arrays

Post by dunbarx » Fri Apr 05, 2019 4:14 am

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

Post by ValiantCuriosity » Fri Apr 05, 2019 6:04 am

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
   ask "What is your name?"
   put it into gName
   answer "Hello " & 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.
https://sites.google.com/a/pgcps.org/li ... /variables

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

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

Re: Trying to understand arrays

Post by redfield » Fri Apr 05, 2019 6:56 pm

Wow thanks everybody for the warm welcome and all the answers! :D
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: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Trying to understand arrays

Post by bogs » Fri Apr 05, 2019 9:29 pm

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.

Enjoy the adventure :)
Image

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”