Need help with arrays
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Need help with arrays
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?
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?
Re: Need help with arrays
Using the latest stable version of LC Community 6.7.x on Win 7 Home Premium, 64bit
Re: Need help with arrays
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
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
Re: Need help with arrays
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:
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
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
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
Re: Need help with arrays
Thanks everyone! Very helpful for my reading!
Re: Need help with arrays
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.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: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.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
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
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
Code: Select all
repeat for each word tWord in temp
Re: Need help with arrays
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:
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
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
Craig
Re: Need help with arrays
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:
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.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
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
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]
Re: Need help with arrays
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
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
Re: Need help with arrays
There is also this page about arrays: http://livecode.wikia.com/wiki/Arrays 

Livecode Wiki: http://livecode.wikia.com
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w
My blog: https://livecode-blogger.blogspot.com
To post code use this: http://tinyurl.com/ogp6d5w