42 — Word count: how often each word appears
"How often each word appears" is one number under each name, which makes it a record. There is only one hard part: a word seen for the first time and a word seen again have to be handled differently.
Steps
Split the text into words:
run it →set text to apple pear apple plum set words to text split by space show how many wordsYou get
4. A word that appears twice is in the list twice.A word seen for the first time starts at one:
run it →set counts to an empty record put apple at 1 in counts show apple in countsA word seen again is taken out, raised, and put back. Three steps:
run it →set counts to an empty record put apple at 1 in counts set seen to apple in counts add 1 to seen put apple at seen in counts show apple in countsYou get
2.What tells the two apart is whether it is already there:
run it →set counts to an empty record set word to apple if counts contains word show this is not the first time else show this is the first time endthis is the first time.The whole thing — one word at a time, one of the two things each time:
run it →set text to apple pear apple plum pear apple set words to text split by space set counts to an empty record for each word in words if counts contains word set seen to word in counts add 1 to seen put word at seen in counts else put word at 1 in counts end end for each word in counts show word show word in counts end show how many countsapple 3,pear 2,plum 1, and the final3is how many different words there were.
Try it yourself
The same word in capitals is counted separately. Putting word in small letters into a name and counting that instead brings them together. Then show
only the words seen more than once by testing the count in the last loop.
What you learned
- One number under each name is a record.
containstells a first sighting from a repeat.- Raising a number inside a record is take-out, add, put-back.
how manyon a record is the number of different names, not of items counted.