Array question - User Guide page 191
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
-
- Posts: 919
- Joined: Wed Nov 04, 2009 11:41 am
Array question - User Guide page 191
Hi,
I'm still in my first week with the demo version of RevTalk and have progressed about half way through the user guide. I need some help with how arrays operate :
The following code is taken from page 191 of the user guide:
on mouseUp
repeat for each word tword in field "sample text"
add 1 to tWordcount[tword]
end repeat
combine twordcount using return and comma
answer tWordcount
end mouseUp
Can someone explain in simple terms how this code works as I don't see how the array gets populated with the words and word count or how revolution knows that two elements are required. Also is the 'combine' command re-allocating the array to twordcount.
Thanks and sorry for the 'stupid' questions...
I'm still in my first week with the demo version of RevTalk and have progressed about half way through the user guide. I need some help with how arrays operate :
The following code is taken from page 191 of the user guide:
on mouseUp
repeat for each word tword in field "sample text"
add 1 to tWordcount[tword]
end repeat
combine twordcount using return and comma
answer tWordcount
end mouseUp
Can someone explain in simple terms how this code works as I don't see how the array gets populated with the words and word count or how revolution knows that two elements are required. Also is the 'combine' command re-allocating the array to twordcount.
Thanks and sorry for the 'stupid' questions...
best wishes
Skids
Skids
Simon,
tword will be a key of the array which is simply the word (this is the associative array thing, almost anything can be a key). And then it adds 1 to the content of key-tword. So the first time around with no entry tword it will add 1, effectively setting the content of tword to 1. If the word tword comes up again then it does not create a new key since this key already exists and adds 1 to the existing key raising the content to 2. And so on.
The beauty is you dont have to worry about creating the keys, it is all automatically done and you have an array whose keys consist of all the words of the text. You access this line delimited list by e.g. saying
when you do you turn the array into a list of the word followed by a comma and then the wordcount. After combining an array it is no longer an array but a normal variable. After combining the statement "tWordCount is an array" would return false. Look for the split command for the inverse: turning a list into an array.
I hope this makes any sense
regards
Bernd
well, "add 1 to tWordCount[tword]" creates an array tWordcount and creates an entry in this array [tword]repeat for each word tword in field "sample text"
add 1 to tWordcount[tword]
tword will be a key of the array which is simply the word (this is the associative array thing, almost anything can be a key). And then it adds 1 to the content of key-tword. So the first time around with no entry tword it will add 1, effectively setting the content of tword to 1. If the word tword comes up again then it does not create a new key since this key already exists and adds 1 to the existing key raising the content to 2. And so on.
The beauty is you dont have to worry about creating the keys, it is all automatically done and you have an array whose keys consist of all the words of the text. You access this line delimited list by e.g. saying
Code: Select all
put the keys of tWordCount into tAllTheWords
Code: Select all
combine twordcount using return and comma
I hope this makes any sense
regards
Bernd
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Arrays in revTalk are basically Maps - each 'key' points to a 'value'. So if you do:
you'll have two items in your array, and key "foo" will point to value 1, and key "bar" will point to value "smurf".
There's no need to declare the array or initiate it to a certain size; it's all done transparently for you.
Since version 3.0, we gained the ability to nest arrays in another array. So you can do things like:
and you'll have an array where the key "1" points to another array, where the key "foo" points to value 1 and key "bar" points to value "smurf"; and in the outer array, the key "2" points to another array, where the key "foo" points to value 99.
Now you can also copy nested arrays as a whole:
and now you have an additional key "3" in your outer array, with a copy of the nested array that was in key "1" - no need to copythe individual items of the nested array.
You can also delete items from the array:
will erase the entire content of the key "2" of your outer array, and this will automatically delete the nested array. No need to 'free' the individual nested items yourself, no chance of memory leaks.
This system is extremely flexible, but takes a little getting used to.
Back to the code snippet in the User Guide.
The repeat for each <chunktype> <chunkVariable> in <container>loop is a very powerful construct in the revTalk language, as it efficiently goes through the entire container variable, and copies the appropriate chunks into the chunkVariable, one at a time.
So what the code snippet does, is go through the text in field "sample text", and copies each word into the variable tword, running the script inside the repeat loop. As explained above, you don't need to allocate the memory, and new items are automatically added to the tWordcount array.
Also, one of the interesting aspects of how the revEngine works, is that if a variable is empty, and you add 1 to it, the assumption is that the variable was supposed to contain 0 and so you get 1 after the addition; the next time around it has 1 so adding 1 means the array item now has value 2.
Assuming we have a text like this:
then at the end of the repeat loop, the array tWordcount will look something like this:
What it does, is go through eack key, and then concatenate its value, putting a comma between the key and the value of the item, and a return in between two items. So you'll end up with a single string something like:
I typed it up in the order that made most sense, but it's going to be in a different order. So you may want to sort the lines in a way that makes more sense.
For instance, if you want to sort it in reverse alphabetical order on the words, you could use:
and then the variable's lines will be sorted that way.
If you want to see the lines in the order of the number of times they appeared in the container variable, you could use:
and then the most used words bubble to the top of the text string.
HTH,
Jan Schenkel.
Code: Select all
put 1 into tArray["foo"]
put "smurf" into tArray["bar"]
There's no need to declare the array or initiate it to a certain size; it's all done transparently for you.
Since version 3.0, we gained the ability to nest arrays in another array. So you can do things like:
Code: Select all
put 1 into tArray[1]["foo"]
put "smurf" into tArray[1]["bar"]
put 99 into tArray[2]["foo"]
Now you can also copy nested arrays as a whole:
Code: Select all
put tArray[1] into tArray[3]
You can also delete items from the array:
Code: Select all
delete variable tArray[2]
This system is extremely flexible, but takes a little getting used to.
Back to the code snippet in the User Guide.
The repeat for each <chunktype> <chunkVariable> in <container>loop is a very powerful construct in the revTalk language, as it efficiently goes through the entire container variable, and copies the appropriate chunks into the chunkVariable, one at a time.
So what the code snippet does, is go through the text in field "sample text", and copies each word into the variable tword, running the script inside the repeat loop. As explained above, you don't need to allocate the memory, and new items are automatically added to the tWordcount array.
Also, one of the interesting aspects of how the revEngine works, is that if a variable is empty, and you add 1 to it, the assumption is that the variable was supposed to contain 0 and so you get 1 after the addition; the next time around it has 1 so adding 1 means the array item now has value 2.
Assuming we have a text like this:
Code: Select all
revTalk is fast and easy and it is simply amazing
- tWordcount["revTalk"] = 1
- tWordcount["is"] = 2
- tWordcount["fast"] = 1
- tWordcount["and"] = 2
- tWordcount["easy"] = 1
- tWordcount["it"] = 1
- tWordcount["simply"] = 1
- tWordcount["amazing"] = 1
What it does, is go through eack key, and then concatenate its value, putting a comma between the key and the value of the item, and a return in between two items. So you'll end up with a single string something like:
Code: Select all
revTalk,1
is,2
fast,1
and,2
easy,1
it,1
simply,1
amazing,1
For instance, if you want to sort it in reverse alphabetical order on the words, you could use:
Code: Select all
sort lines of tWordcount descending by item 1 of each
If you want to see the lines in the order of the number of times they appeared in the container variable, you could use:
Code: Select all
sort lines of twordcount numeric descending by item 2 of each
HTH,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
-
- Posts: 919
- Joined: Wed Nov 04, 2009 11:41 am