Re: Non-executed repeat loop robs variable of value/definition
Re: Non-executed repeat loop robs variable of value/definition
- Subject: Re: Non-executed repeat loop robs variable of value/definition
- From: deivy petrescu <email@hidden>
- Date: Sun, 28 Aug 2005 14:46:56 -0400
On Aug 28, 2005, at 13:14, kai wrote:
On 28 Aug 2005, at 16:54, deivy petrescu wrote:
kai,
I have the same results. But something worse happens here.
<script>
set n to 1
if n ≠ 1 then
log n
log "this"
set k to true
else
log n -- 1
log "that" -- "that"
repeat with n from 1 to 10
end repeat
end if
n
-->10
</script>
which is opposite of what I would expect following your examples.
Actually, I don't find that quite so surprising, Deivy. With the
variable 'n' having a value of 1, the statement "n ≠ 1" would
certainly evaluate to false - although any statements within the
"else" section would therefore be executed as a consequence of a
'true' evaluation. This might be distilled down to:
You are right! I was narrowly thinking of a false first evaluation.
Now for the nightmare:
<script>
set n to 1
if n = 1 then
log n -- 1
log "this" -- "this"
set k to true
else
log n
log "that"
repeat with n from 1 to 10
end repeat
end if
n
--> "AppleScript Error: The variable n is not defined."
</script>
Right. This is pretty much (give or take) what I found. Again,
boiling it down:
----------
set n to 1
if true then
-- do nothing/something else
else -- false
repeat with n from 1 to 10
end repeat
end if
n
--> "AppleScript Error: The variable n is not defined."
----------
...or:
----------
set n to 1
if false then repeat with n from 1 to 10
end repeat
n
--> "AppleScript Error: The variable n is not defined."
----------
Actually, I wanted to disagree with you, but I see the point. It is
the same thing
By the way, thanks for spoiling my weekend!!!
:)
It's the least I could do, Deivy. In fact, I consider it my bounden
duty. ;)
Thanks for your reply!
---
kai
Now that you've done it, here is what ASLG has to say about it (p 255):
"You can use an existing variable as the loop variable in a Repeat
statement or
define a new one in the Repeat statement. In either case, the loop
variable is
defined outside the loop. You can change the value of the loop
variable inside..."
So technically either way you should get a value for "n".
Now some more strange things (at least for now)
Running the script in Smile, in a script window I get the same result.
Running the script in the text window (which is also called
AppleScript terminal) you get the same result. However, if you break
the script down in the text window, you run everything, no errors.
so first one executes
set n to 1
then one executes
if false then
repeat with n from 6 to 7
end repeat
end if
finally,
n
--> n=1
Well, I believed that the first execution makes n into a global
variable.
So I tried your original script with global and property and got the
same error. I think that the second statement in Smile, the "if" is
local, so "n" dies after the loop in the text window. Which is not
the case in AS.
deivy
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-users mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden