Mailing Lists: Apple Mailing Lists

Image of Mac OS face in stamp
 
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Variables in loop



Title: Re: Variables in loop
On 3/29/05 12:26 AM, "Jasper Van Proeyen" <email@hidden> wrote:


I'm trying to set some variables as follows:

setTheVariables({{variable1, 10}, {variable2, 3}, {variable3, (variable1 - variable2)}, {variable4, true}})

-- Set the given variables
on setTheVariables(listOfVariables)
    set countVariables to count items in listOfVariables
    repeat with i from 1 to countVariables
        set (item 1 of item i of listOfVariables) to (item 2 of item i of listOfVariables)
    end repeat
end setTheVariables

However, this code doesn't seem to work... Is it just not possible to set variables in a loop or am I doing something wrong?

  1. You're writing to the wrong list. This has nothing to do with AppleScript Studio - you should be writing to the AppleScript-Users list.
  2. There's nothing wrong with the handler at all. It works just fine.
  3. When you write to anyone, including a mailing list, it's pointless to say "it doesn't work". Report the error you're getting. What error are you getting?
  4. Include enough code to be meaningful, and explain what you expect to get.
  5. Naturally, you'll get a "The variable variable1 is not defined" error if you don't define all the variable in the calling command before trying to run the script. Is that what you're getting?
  6. If you set all the variables first,  and then return the whole list. e.g.:

set variable1 to 1
set variable2 to 2
set variable3 to 3
set variable4 to 4

setTheVariables({{variable1, 10}, {variable2, 3}, {variable3, (variable1 - variable2)}, {variable4, true}}
--> {{10, 10}, {3, 3}, {-1, -1}, {true, true}}

-- Set the given variables
on setTheVariables(listOfVariables)
    set countVariables to count items in listOfVariables
   repeat with i from 1 to countVariables
       set (item 1 of item i of listOfVariables) to (item 2 of item i of listOfVariables)
    end repeat
   return listOfVariables
end setTheVariables



it works just fine:

--> {{10, 10}, {3, 3}, {-1, -1}, {true, true}}

Note that you have to

     return listOfVariables

if you want to inspect the list.


    7. If what you're really trying to do is construct a new command containing variable names, you're not going to be able to do it. As soon as you start accessing the variables they will resolve to their values. That's how AppleScript works. And that's why you have to define some values to begin with or the script won't run.

--
Paul Berkowitz
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Applescript-studio mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/applescript-studio/email@hidden

This email sent to email@hidden

References: 
 >Variables in loop (From: Jasper Van Proeyen <email@hidden>)



Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2007 Apple Inc. All rights reserved.