sashaosonia

Q: variable name of variable

Hello all (especially @Niel & @Stivejobstan123 & @Camelot)

 

next question by beginner:

 

can I ask (and, actually, create)  a variable by name in this way:

 

repeat 3 times

   set i to 1

   set a( i )to i*6

   set i to i+1

end repeat

 

return a(2)

return a(x) etc.


PS I know about "on something () - end something"


but question about variable

MacBook Pro, OS X El Capitan (10.11.2), null

Posted on Jul 5, 2016 7:28 PM

Close

Q: variable name of variable

  • All replies
  • Helpful answers

  • by Camelot,

    Camelot Camelot Jul 5, 2016 11:30 PM in response to sashaosonia
    Level 8 (47,285 points)
    Mac OS X
    Jul 5, 2016 11:30 PM in response to sashaosonia

    I'm not entirely sure what you hoped your code would do, but if you're asking about creating dynamically named variables, the short answer is no - variables must be defined at compile time and you cannot create dynamic variables at runtime.

     

    The sole(?) exception to this is to create one variable as a list object and populate that with your dynamically-created variables, but that might not fit your needs.

  • by Hiroto,Solvedanswer

    Hiroto Hiroto Jul 7, 2016 9:35 AM in response to sashaosonia
    Level 5 (7,286 points)
    Jul 7, 2016 9:35 AM in response to sashaosonia

    Hello

     

    Perhaps what you want is list which is an ordered collection of items.

     

     

    set a to {}
    repeat with i from 1 to 3
        set a's end to i * 6
    end repeat
    return a
    --return a's item 2
    

     

     

    If you're indeed looking for something like hash table (associative array), you might try the code listed in the following thread.

     

    Set an indeterminate number of variables

    https://discussions.apple.com/thread/7000650

     

     

    Good luck,

    H