Monday, December 11, 2017

#1 2016-07-06 07:57:55 am

lowjackson
Member
Registered: 2006-02-08
Posts: 60

Updating variables in handlers & repeat loops

Hi All

When I run the following script 'x' returns…

(*2*)
(*2*)
(*2*)
(*2*)
(*2*)

but I was expecting…

(*2*)
(*3*)
(*4*)
(*5*)
(*6*)

Applescript:


set x to 1

repeat 5 times    
   addOne(x)
end repeat

on addOne(x)
   set x to x + 1
   log x
end addOne

I'm guessing that I need to return 'x' back to the repeat loop, but I don't know how?

Thanks again

Shane

Offline

 

#2 2016-07-06 08:07:55 am

Woggledog
Member
Registered: 2012-12-06
Posts: 150

Re: Updating variables in handlers & repeat loops

Applescript:

set x to 1

repeat 5 times
   set x to addOne(x)
end repeat

on addOne(y)
   set y to y + 1
   log y
   return y
end addOne

Offline

 

#3 2016-07-06 08:13:45 am

lowjackson
Member
Registered: 2006-02-08
Posts: 60

Re: Updating variables in handlers & repeat loops

thank you!

Offline

 

#4 2016-07-06 09:02:52 am

DJ Bazzie Wazzie
Member
From:: the Netherlands
Registered: 2004-10-20
Posts: 2726
Website

Re: Updating variables in handlers & repeat loops

Or using references:

Applescript:


set x to 1

repeat 5 times
   addOne(a reference to x)
end repeat

on addOne(x)
   set contents of x to x + 1
   log x
end addOne

Offline

 

Board footer

Powered by FluxBB

RSS (new topics) RSS (active topics)