Need help with 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
p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Need help with arrays

Post by p0ntif » Mon Jul 14, 2014 4:39 pm

Hello all, I am on my sojourn to learn live code, and hopefully eventually become fluent in it.
I have just recently completed the Livecode university, which I cannot compliment enough. Great resource for absolute beginners like myself.

However, there wasn't much of a tutorial on arrays. Can someone direct me to a resource where I can learn how to use arrays?

magice
Posts: 457
Joined: Wed Mar 18, 2009 12:57 am

Re: Need help with arrays

Post by magice » Mon Jul 14, 2014 9:18 pm


keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: Need help with arrays

Post by keram » Tue Jul 15, 2014 11:31 am

Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

keram
Posts: 340
Joined: Fri Nov 08, 2013 4:22 am

Re: Need help with arrays

Post by keram » Tue Jul 15, 2014 11:49 am

Also the Starter Kit stack has got a card about arrays; download from this page:
http://tlittle72.tripod.com/info.html

and have a look here: http://livecode.wikia.com/wiki/Arrays
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit

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

Re: Need help with arrays

Post by dunbarx » Tue Jul 15, 2014 2:27 pm

All good stuff. But I think you should make your own arrays and experiment with them. I will give you two, which are generically old hat around here, but will be very good teaching tools. Try this on a new card with a button and a fairly large field named "wordList". In the button script:

Code: Select all

on mouseup --creates an array with each element twice its key
   put "" into fld "wordList"
   repeat with y = 1 to 10
      put 2 * y into tArray[y}
   end repeat
 answer the keys of tArray
   
   combine tArray with return and comma
   sort tArray numeric by item 2 of each
   put tArray after  fld "wordList"
end mouseup

on mouseUp --finds the number of instances of a word
   put "" into fld "wordList"
   repeat 100
      put any word of "one two buckle my shoe" & space after temp
   end repeat
   put temp into line 1 of fld "wordList"
   repeat for each word tWord in temp
      add 1 to tWordCount[tWord] --ADD 1 TO THE ELEMENT OF A KEY
   end repeat
   answer tWordCount["buckle"]
end mouseup
Comment out one handler or the other. You may want to step through these to see what is going on. One nice thing about the debugger is that it displays arrays in their entirety, which is not readily seeable in the IDE.

In the first handler, do you see how an array is created on the fly, and how it is populated with a value that doubles the loop index? When you see the keys of that array, note that these are the default keys, which will be numeric (note they are not sorted). They can in fact be anything, but here they are integers because we did not expressly name them.

For the second handler, do you see the three basic steps here? First a random list is created using a handful of words. Then an array is created on the fly (the "add" line), where an associated value is incremented for each key (here we have indeed named them) of that array. And finally, we find the value associated with an arbitrary key (one of those words).

You simply must modify these until you get how they work. Make your own arrays. It does not matter how silly or simple they are, or how useless. The practice is, er, key.

Simon, these are practice gadgets, not really full blown answers.

Craig Newman

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: Need help with arrays

Post by p0ntif » Tue Jul 15, 2014 2:46 pm

Thanks everyone! Very helpful for my reading!

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: Need help with arrays

Post by p0ntif » Tue Jul 15, 2014 3:36 pm

dunbarx wrote:All good stuff. But I think you should make your own arrays and experiment with them. I will give you two, which are generically old hat around here, but will be very good teaching tools. Try this on a new card with a button and a fairly large field named "wordList". In the button script:

Code: Select all

on mouseup --creates an array with each element twice its key
   put "" into fld "wordList"
   repeat with y = 1 to 10
      put 2 * y into tArray[y}
   end repeat
 answer the keys of tArray
   
   combine tArray with return and comma
   sort tArray numeric by item 2 of each
   put tArray after  fld "wordList"
end mouseup

on mouseUp --finds the number of instances of a word
   put "" into fld "wordList"
   repeat 100
      put any word of "one two buckle my shoe" & space after temp
   end repeat
   put temp into line 1 of fld "wordList"
   repeat for each word tWord in temp
      add 1 to tWordCount[tWord] --ADD 1 TO THE ELEMENT OF A KEY
   end repeat
   answer tWordCount["buckle"]
end mouseup
Comment out one handler or the other. You may want to step through these to see what is going on. One nice thing about the debugger is that it displays arrays in their entirety, which is not readily seeable in the IDE.

In the first handler, do you see how an array is created on the fly, and how it is populated with a value that doubles the loop index? When you see the keys of that array, note that these are the default keys, which will be numeric (note they are not sorted). They can in fact be anything, but here they are integers because we did not expressly name them.

For the second handler, do you see the three basic steps here? First a random list is created using a handful of words. Then an array is created on the fly (the "add" line), where an associated value is incremented for each key (here we have indeed named them) of that array. And finally, we find the value associated with an arbitrary key (one of those words).

You simply must modify these until you get how they work. Make your own arrays. It does not matter how silly or simple they are, or how useless. The practice is, er, key.

Simon, these are practice gadgets, not really full blown answers.

Craig Newman
Thanks for the practice gadgets! I totally get the first handler. I think the syntax on the second handler is a bit confusing to me.
Can you explain to me why you call the variable "temp"? It doesn't make sense to me that when you write

Code: Select all

   repeat 100
      put any word of "one two buckle my shoe" & space after temp
   end repeat
Does the after command put all of those words into temp? Because then you

Code: Select all

repeat for each word tWord in temp
- so I presume that by using the command "after" it actually puts it into temp? Sorry if that sounds like rambling. Just trying to understand that piece.

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

Re: Need help with arrays

Post by dunbarx » Tue Jul 15, 2014 4:27 pm

Hi.

You are tackling arrays, but do not know how to populate a variable. OK.

The word "into", as in:

put someValue into someVariable

must be familiar, no? "After", and "before" are similar keywords, and work just as they ought to:

put "yy" after "xx" --yields "xxyy"
put space & "yy" after "xx" -- yields "xx yy"
put "xx" & space before "yy" -- yields "xx yy"

So in the loop, I am taking a random word from a literal and concatenating that word, and a space, "onto" a variable. Step through this. Watch the variable "temp".

As for my very first line in this reply, I am only saying that you need to be more conversant with the utter basics of liveCode. You are at the same time dealing with quite intermediate topics.

Homework. Please comment on this, related to your comment about why we need "temp" at all:

Code: Select all

on mouseUp
   repeat 100
      add 1 to tWordCount[any word in "one two buckle my shoe"]
   end repeat
   answer tWordCount["buckle"]
end mouseup
No need for temp, but no list of words either. Is there something lost here, for the sake of brevity? How about if you needed to see or use such a list for another purpose? No right answer to this, but do think about it.

Craig

p0ntif
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 18
Joined: Fri Mar 15, 2013 4:41 pm

Re: Need help with arrays

Post by p0ntif » Wed Jul 16, 2014 2:43 pm

dunbarx wrote:Hi.

You are tackling arrays, but do not know how to populate a variable. OK.

The word "into", as in:

put someValue into someVariable

must be familiar, no? "After", and "before" are similar keywords, and work just as they ought to:

put "yy" after "xx" --yields "xxyy"
put space & "yy" after "xx" -- yields "xx yy"
put "xx" & space before "yy" -- yields "xx yy"

So in the loop, I am taking a random word from a literal and concatenating that word, and a space, "onto" a variable. Step through this. Watch the variable "temp".

As for my very first line in this reply, I am only saying that you need to be more conversant with the utter basics of liveCode. You are at the same time dealing with quite intermediate topics.

Homework. Please comment on this, related to your comment about why we need "temp" at all:

Code: Select all

on mouseUp
   repeat 100
      add 1 to tWordCount[any word in "one two buckle my shoe"]
   end repeat
   answer tWordCount["buckle"]
end mouseup
No need for temp, but no list of words either. Is there something lost here, for the sake of brevity? How about if you needed to see or use such a list for another purpose? No right answer to this, but do think about it.

Craig

Thanks for clarifying, that makes sense. You are right, I cannot seem to reconstruct the list of words. I can display how many of each within the array by using

Code: Select all

combine with space and tab
and putting it into a field, but cannot seem to rebuild the list as it was formed.
However, I wonder if while you are populating the array in the original repeat loop, if you could "tag" each entry maybe by adding a key (setting a new variable count to zero and then incrementing for the 100 repeats)? For example something like this: tWordCount [count][any word in "one two buckle my shoe"] and then rebuilding the list somehow in order? Although i am not sure how to go about doing this.


[/code]

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

Re: Need help with arrays

Post by dunbarx » Wed Jul 16, 2014 6:17 pm

Hi.

These sort of shenanigans are just what you should be thinking about; programming sideshows that twist and turn LC and its innards. It will surely make you stronger.

Getting the "list" back from the array? Making the list first and using it to make the array? Who cares? In one sense it is a matter of style. In another, you might find one far more robust than the other. Only one way to find out.

Keep at it. Write back often.

Craig

MaxV
Posts: 1580
Joined: Tue May 28, 2013 2:20 pm
Contact:

Re: Need help with arrays

Post by MaxV » Thu Jul 17, 2014 4:16 pm

There is also this page about arrays: http://livecode.wikia.com/wiki/Arrays :D
Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w

Post Reply