Page 1 of 1

Trying to understand arrays

Posted: Thu Apr 04, 2019 1:59 pm
by redfield
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!

Re: Trying to understand arrays

Posted: Thu Apr 04, 2019 2:47 pm
by bogs
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.

Re: Trying to understand arrays

Posted: Thu Apr 04, 2019 2:54 pm
by Thierry
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

Re: Trying to understand arrays

Posted: Thu Apr 04, 2019 3:00 pm
by FourthWorld
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

Re: Trying to understand arrays

Posted: Thu Apr 04, 2019 5:02 pm
by dunbarx
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:

Re: Trying to understand arrays

Posted: Thu Apr 04, 2019 5:52 pm
by dunbarx
RedField.
Sorry for surely stupid beginner questions
LiveCode is never having to say you are sorry.

Craig

Re: Trying to understand arrays

Posted: Fri Apr 05, 2019 1:08 am
by FourthWorld
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. :)

Re: Trying to understand arrays

Posted: Fri Apr 05, 2019 4:14 am
by dunbarx
Richard.

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

Craig

Re: Trying to understand arrays

Posted: Fri Apr 05, 2019 6:04 am
by ValiantCuriosity
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

Re: Trying to understand arrays

Posted: Fri Apr 05, 2019 6:56 pm
by redfield
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 :( ).

Re: Trying to understand arrays

Posted: Fri Apr 05, 2019 9:29 pm
by bogs
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 :)