can someone explain WHY this code doesn't work?

hi everyone,
New here and new to applescript. I’m reading through a book that thus far has been very informative. While going through a few of the examples, I ran across one that doesn’t seem to work. This code is supposed to create a list of 10 numbers, adding one number to the empty list, until the list reaches 10 numbers, then the repeat is supposed to stop. When I run this script, the list that is created each time is only one number, and never gets bigger then that. Can anyone explain why? Sorry for the probably VERY noobish question. I’m just getting into applescript.

Thanks!

set listValues to {}
set blnX to true

repeat while blnX = true
	copy (some item of {1, 2, 3, 4, 5}) to end of listValues
	set blnX to ((count listValues) > 10)
end repeat
listValues

Hi. Welcome to MacScripter.

At the end of the first iteration of your repeat, listValues only contains one item, so ‘((count listValues) > 10)’ is false, binX gets set to ‘false’, and the next iteration doesn’t take place.

Another thing is that ‘((count listValues) > 10)’ won’t be true until the number of items is 11, which is more than you want.

These do what’s required:

set listValues to {}

repeat until ((count listValues) = 10)
	set end of listValues to (some item of {1, 2, 3, 4, 5})
end repeat
listValues

Or:

set listValues to {}

repeat 10 times
	set end of listValues to (some item of {1, 2, 3, 4, 5})
end repeat
listValues

Edit: Explanation rewritten.

Maybe I’m obtuse (okay, I am obtuse :P) but a change in one character should fix this.

set blnX to ((count listValues) < 10), changing the “>” to “<”. I think this was a clerical error.:smiley: blnX will resolve to true while the count is less than 10, hence the repeat will function, filling the list until blnX is equal to 10.

That being said, Nigel’s suggestions are more basic and understandable. Caution should be exercised with “Repeat while.” loops. It’s easier to get into an infinite loop than an explicit declaration.

Have fun!
Jim Neumann
BLUEFROG