Inner loop accepts values ahead of natural sequence of actions - how can that be?
Inner loop accepts values ahead of natural sequence of actions - how can that be?
- Subject: Inner loop accepts values ahead of natural sequence of actions - how can that be?
- From: ILJA SHEBALIN <email@hidden>
- Date: Sat, 10 Jun 2017 14:30:10 +0300
Hello,
I'm recently trying to figure out a logic behind the design of a script which
contains two nested loops. The script counts the number of repeating words of a
text and puts the information in a list of records each containing a label
storing the word's textual value and another label that stores the number of
repetitions of the word in the text.
The script:
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
repeat with record_ref in word_frequency_list --THE QUESTIONABLE LOOP
(word_frequency_list is a list that contains records). Its position is closer
to the opening 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
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
end repeat
get word_frequency_list
What bugs me is that it manages to escape error messages since the inner loop
contains variables ought to be deemed as undefined (the_word variable in
QUESTIONABLE LOOP was NOT defined explicitly before the inner loop entered the
game). How's that? Nevertheless it works.
If I modify the script so that the inner loop's position goes later in time
(see below) then it outputs a list of records each containing words just one at
a time without collecting it in a single record, i.e., skipping "else
set the_count of word_info to (the_count of word_info) + 1"
part of conditional statement of the outer loop with only the main condition
considered. I thought AppleScript's statements are executed consecutively from
top to bottom, thus the_word should be defined earlier but when I run other
version of same script as shown it generates the results I don't want.
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
What is the mechanics?
_______________________________________________
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