Syntax for incrementing a numeric 'word'
ken at kencorey.com
Wed Jun 27 03:58:09 CDT 2012
I built a label with multiple lines, put "1 2 3/n2 3 4/n3 4 5" into it
and ran this line of code on it:
add 1 to word 2 of line 2 of field "field"
It worked just as you'd expect it to.
However, this seems a very slow way to do it.
Hashes work a treat for this kind of thing.
-- keep track of how many times each word is used.
repeat for each word tWord in field "txt"
add 1 to tHash[tWord]
-- print overview
put the number of lines in field "txt" into field "numlines"
put the number of words in field "txt" into field "numwords"
put the number of lines in the keys of tHash into field "numuniq"
-- print details
repeat for each line tLine in the keys of tHash
put tLine&" "&tHash[tLine]&CR after field "result"
Simpler, and I'd wager significantly faster.
On 27/06/2012 09:24, Keith Clarke wrote:
> Hi folks,
> I'm working on a simple script to identify and count the unique words dumped into a 'Source' field to create a 'Results' list field with the word and counter.
> However, this script is throwing errors at the line when I attempt to increment the second word by adding 1 to it. What's wrong with the syntax for incrementing the counter chunk?
> on mouseUp
> put empty into field "Results"
> repeat for each word tWord in field "Source"
> if field "Results" is empty then put tWord & tab & 1 after field "Results"
> if tWord is among the words of field "Results" then
> repeat for each line tLine in field "Results"
> if word 1 of tLine <> tWord
> then exit repeat
> else add 1 to word 2 of line tLine in field "Results"
> end repeat
> else put tWord & cr after field "Results"
> end repeat
> end mouseUp
> use-livecode mailing list
> use-livecode at lists.runrev.com
> Please visit this url to subscribe, unsubscribe and manage your subscription preferences:
More information about the use-livecode