#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2006
    Posts
    63
    Rep Power
    10

    How to concatenate a variable name?


    I'm having trouble concatenating a variable name, I'm sure I'm just missing something basic.

    Say I have a variable named "F1_run" whose value is "something", and a variable called key whose value is "F1".

    Without writing an if statement, how would I assign the value of a new variable to the value of F1_run?

    For example, if F1_run = "something" and "key" = "F1", I'd like this statement to assgn the value of new_var to "something":

    new_var = key + "_run"

    However, in the above statement, instead of assigning new_var to the value of F1_run, it instead assigns it to the text string "F1_run".

    On the off chance anyone followed this description, any tips?
  2. #2
  3. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Oh wow, you lost me. I've re-read your post multiple times and I still do not follow. Is this what you want...?

    Code:
    new_var = F1_run
    That's what it is sounding like to me.
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Variables names in Python are not symbols and shouldn't be treated as such however you can use the local and global scopes, which are actually dictionaries with strings for keys to do something similar.

    Code:
    >>> locals()
    {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None}
    >>> globals()
    {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', '__doc__': None}
    >>> a = 1
    >>> b = 2
    >>> globals()["a" + "b"] = 2
    >>> ab
    2
    >>>
    I would repeat however that variables aren't symbols and I most certainly wouldn't use them as such. If you want that best look somewhere else because the hack wont be do ugly .

    Note: this isn't exactly considered a safe thing to do either.

    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. Mini me.
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Location
    Cambridge, UK
    Posts
    783
    Rep Power
    13
    Here is a third possible interpretation
    python Code:
    >>> F1_run = "something"
    >>> key = "F1"
    >>> new_var = eval(key+"_run")
    >>> new_var
    'something'
    >>>


    grim
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Nov 2003
    Posts
    624
    Rep Power
    34
    I'm having trouble concatenating a variable name, I'm sure I'm just missing something basic.
    Yes - the use of dictionaries for things like that...

    For example, if F1_run = "something" and "key" = "F1", I'd like this statement to assgn the value of new_var to "something":

    new_var = key + "_run"
    Code:
    LinkKeysToActions = {}
    LinkKeysToActions['F1_run'] = "something"
    
    key="F1"
    
    new_var = LinkKeysToActions[key+"_run"]
    new_var will end up as "something".

    Or you could make the key a tuple, e.g. ('F1', 'run')
    Or you could work a dictionary per action, e.g. run["F1"]

    Comments on this post

    • CountVikernes agrees : Hah! Indeed!

IMN logo majestic logo threadwatch logo seochat tools logo