Re: Inner loop accepts values ahead of natural sequence of actions - how can that be?
Re: Inner loop accepts values ahead of natural sequence of actions - how can that be?
- Subject: Re: Inner loop accepts values ahead of natural sequence of actions - how can that be?
- From: Yvan KOENIG <email@hidden>
- Date: Sat, 10 Jun 2017 19:19:52 +0200
>
>
> set the_text to "It was the best of times, it was the worst of times, it was
> the age of wisdom, it was the age of foolishness, it was the epoch of belief,
> it was the epoch of incredulity, it was the season of Light, it was the
> season of Darkness, it was the spring of hope, it was the winter of despair,
> we had everything before us, we had nothing before us, we were all going
> direct to Heaven, we were all going direct the other way—in short, the period
> was so far like the present period, that some of its noisiest authorities
> insisted on its being received, for good or for evil, in the superlative
> degree of comparison only."
>
>
> set word_list to every word of text of the_text
> set word_frequency_list to {}
> repeat with every_word_ref in word_list
> set the_current_word to contents of every_word_ref
>
> set word_info to missing value
>
>
> if word_info = missing value then --word_info is a record
> set word_info to {the_word:the_current_word, the_count:1}
> set end of word_frequency_list to word_info
> else
> set the_count of word_info to (the_count of word_info) + 1
> end if
>
>
> repeat with record_ref in word_frequency_list --THE QUESTIONABLE LOOP
> (word_frequency_list is a list that contains records). Now its position is
> moved at the back, closer to the exit of the outer loop
> if the_word of record_ref = the_current_word then set word_info
> to contents of record_ref
> #exit repeat -- I discovered it's redundant since the
> script does without it.
> end if
> end repeat
> end repeat
> get word_frequency_list
In this second version you create a new record for every word. You get eleven
times the record {the_word:"was", the_count:1} when the first one returns -
more efficiently - a single record {the_word:"was", the_count:11}
It's quite easy to understand.
I extract a group of instructions :
set word_info to missing value
if word_info = missing value then -- this condition is ALWAYS matched
set word_info to {the_word:the_current_word, the_count:1}
set end of word_frequency_list to word_info
else
set the_count of word_info to (the_count of word_info) + 1
end if
In fact a bit of cleaning would give:
set word_info to {the_word:the_current_word, the_count:1}
set end of word_frequency_list to word_info
Both versions return:
{{the_word:"It", the_count:1}, {the_word:"was", the_count:1}, {the_word:"the",
the_count:1}, {the_word:"best", the_count:1}, {the_word:"of", the_count:1},
{the_word:"times", the_count:1}, {the_word:"it", the_count:1}, {the_word:"was",
the_count:1}, {the_word:"the", the_count:1}, {the_word:"worst", the_count:1},
{the_word:"of", the_count:1}, {the_word:"times", the_count:1}, {the_word:"it",
the_count:1}, {the_word:"was", the_count:1}, {the_word:"the", the_count:1},
{the_word:"age", the_count:1}, {the_word:"of", the_count:1},
{the_word:"wisdom", the_count:1}, {the_word:"it", the_count:1},
{the_word:"was", the_count:1}, {the_word:"the", the_count:1}, {the_word:"age",
the_count:1}, {the_word:"of", the_count:1}, {the_word:"foolishness",
the_count:1}, {the_word:"it", the_count:1}, {the_word:"was", the_count:1},
{the_word:"the", the_count:1}, {the_word:"epoch", the_count:1}, {the_word:"of",
the_count:1}, {the_word:"belief", the_count:1}, {the_word:"it", the_count:1},
{the_word:"was", the_count:1}, {the_word:"the", the_count:1},
{the_word:"epoch", the_count:1}, {the_word:"of", the_count:1},
{the_word:"incredulity", the_count:1}, {the_word:"it", the_count:1},
{the_word:"was", the_count:1}, {the_word:"the", the_count:1},
{the_word:"season", the_count:1}, {the_word:"of", the_count:1},
{the_word:"Light", the_count:1}, {the_word:"it", the_count:1}, {the_word:"was",
the_count:1}, {the_word:"the", the_count:1}, {the_word:"season", the_count:1},
{the_word:"of", the_count:1}, {the_word:"Darkness", the_count:1},
{the_word:"it", the_count:1}, {the_word:"was", the_count:1}, {the_word:"the",
the_count:1}, {the_word:"spring", the_count:1}, {the_word:"of", the_count:1},
{the_word:"hope", the_count:1}, {the_word:"it", the_count:1}, {the_word:"was",
the_count:1}, {the_word:"the", the_count:1}, {the_word:"winter", the_count:1},
{the_word:"of", the_count:1}, {the_word:"despair", the_count:1},
{the_word:"we", the_count:1}, {the_word:"had", the_count:1},
{the_word:"everything", the_count:1}, {the_word:"before", the_count:1},
{the_word:"us", the_count:1}, {the_word:"we", the_count:1}, {the_word:"had",
the_count:1}, {the_word:"nothing", the_count:1}, {the_word:"before",
the_count:1}, {the_word:"us", the_count:1}, {the_word:"we", the_count:1},
{the_word:"were", the_count:1}, {the_word:"all", the_count:1},
{the_word:"going", the_count:1}, {the_word:"direct", the_count:1},
{the_word:"to", the_count:1}, {the_word:"Heaven", the_count:1}, {the_word:"we",
the_count:1}, {the_word:"were", the_count:1}, {the_word:"all", the_count:1},
{the_word:"going", the_count:1}, {the_word:"direct", the_count:1},
{the_word:"the", the_count:1}, {the_word:"other", the_count:1},
{the_word:"way", the_count:1}, {the_word:"in", the_count:1}, {the_word:"short",
the_count:1}, {the_word:"the", the_count:1}, {the_word:"period", the_count:1},
{the_word:"was", the_count:1}, {the_word:"so", the_count:1}, {the_word:"far",
the_count:1}, {the_word:"like", the_count:1}, {the_word:"the", the_count:1},
{the_word:"present", the_count:1}, {the_word:"period", the_count:1},
{the_word:"that", the_count:1}, {the_word:"some", the_count:1}, {the_word:"of",
the_count:1}, {the_word:"its", the_count:1}, {the_word:"noisiest",
the_count:1}, {the_word:"authorities", the_count:1}, {the_word:"insisted",
the_count:1}, {the_word:"on", the_count:1}, {the_word:"its", the_count:1},
{the_word:"being", the_count:1}, {the_word:"received", the_count:1},
{the_word:"for", the_count:1}, {the_word:"good", the_count:1}, {the_word:"or",
the_count:1}, {the_word:"for", the_count:1}, {the_word:"evil", the_count:1},
{the_word:"in", the_count:1}, {the_word:"the", the_count:1},
{the_word:"superlative", the_count:1}, {the_word:"degree", the_count:1},
{the_word:"of", the_count:1}, {the_word:"comparison", the_count:1},
{the_word:"only", the_count:1}}
The first one return:
{{the_word:"It", the_count:10}, {the_word:"was", the_count:11},
{the_word:"the", the_count:14}, {the_word:"best", the_count:1}, {the_word:"of",
the_count:12}, {the_word:"times", the_count:2}, {the_word:"worst",
the_count:1}, {the_word:"age", the_count:2}, {the_word:"wisdom", the_count:1},
{the_word:"foolishness", the_count:1}, {the_word:"epoch", the_count:2},
{the_word:"belief", the_count:1}, {the_word:"incredulity", the_count:1},
{the_word:"season", the_count:2}, {the_word:"Light", the_count:1},
{the_word:"Darkness", the_count:1}, {the_word:"spring", the_count:1},
{the_word:"hope", the_count:1}, {the_word:"winter", the_count:1},
{the_word:"despair", the_count:1}, {the_word:"we", the_count:4},
{the_word:"had", the_count:2}, {the_word:"everything", the_count:1},
{the_word:"before", the_count:2}, {the_word:"us", the_count:2},
{the_word:"nothing", the_count:1}, {the_word:"were", the_count:2},
{the_word:"all", the_count:2}, {the_word:"going", the_count:2},
{the_word:"direct", the_count:2}, {the_word:"to", the_count:1},
{the_word:"Heaven", the_count:1}, {the_word:"other", the_count:1},
{the_word:"way", the_count:1}, {the_word:"in", the_count:2}, {the_word:"short",
the_count:1}, {the_word:"period", the_count:2}, {the_word:"so", the_count:1},
{the_word:"far", the_count:1}, {the_word:"like", the_count:1},
{the_word:"present", the_count:1}, {the_word:"that", the_count:1},
{the_word:"some", the_count:1}, {the_word:"its", the_count:2},
{the_word:"noisiest", the_count:1}, {the_word:"authorities", the_count:1},
{the_word:"insisted", the_count:1}, {the_word:"on", the_count:1},
{the_word:"being", the_count:1}, {the_word:"received", the_count:1},
{the_word:"for", the_count:2}, {the_word:"good", the_count:1}, {the_word:"or",
the_count:1}, {the_word:"evil", the_count:1}, {the_word:"superlative",
the_count:1}, {the_word:"degree", the_count:1}, {the_word:"comparison",
the_count:1}, {the_word:"only", the_count:1}}
Yvan KOENIG running Sierra 10.12.5 in French (VALLAURIS, France) samedi 10 juin
2017 19:18:47
_______________________________________________
Do not post admin requests to the list. They will be ignored.
AppleScript-Users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
Archives: http://lists.apple.com/archives/applescript-users
This email sent to email@hidden