How to "convert" a string into a variable name?

Discussion in 'Perl Misc' started by Markus Hänchen, Aug 2, 2006.

  1. Hi,

    very simple question:

    $TempOut = 25;
    $name = '$' . 'Temp' . 'Out';
    print $name;

    What do I have to change to get '25' printed and not '$TempOut'? Or is
    this not possible?
    Thanks.

    Markus
     
    Markus Hänchen, Aug 2, 2006
    #1
    1. Advertisements

  2. Markus Hänchen

    Ben Morrow Guest

    This is a FAQ. perldoc -q "variable name".

    Ben
     
    Ben Morrow, Aug 2, 2006
    #2
    1. Advertisements

  3. Found the solution myself:

    $TempOut = 25;
    $name = 'print '. '"$' . 'Temp' . 'Out"';
    eval $name;

    Sorry for bothering you.
    Markus
     
    Markus Hänchen, Aug 2, 2006
    #3
  4. Markus Hänchen

    Paul Lalli Guest

    While it's good that you found *a* solution on your own, I must caution
    you that this is a *bad* solution. Very very bad. eval() is a powerful
    but dangerous function. If at any point, the string you're eval'ing
    contains input from a user, you could do some serious damage to your
    program and to your system.

    A *good* solution is to _not_ try to create a variable name out of a
    string. Instead, store all the "variables" you want to access this way
    in a hash. The keys are whatever you currently have as a variable
    name, and the value is the value you wanted to assign to that variable.
    Using that method, your example above becomes:

    my %value_of
    $value_of{TempOut} = 25;
    $name = 'Temp' . 'Out';
    print $value_of{$name};

    This is all spelled out much more clearly in the FAQ:
    perldoc -q "variable name"

    I strongly suggest you read it.

    Good luck,
    Paul Lalli
     
    Paul Lalli, Aug 2, 2006
    #4
  5. While it's good that you found *a* solution on your own, I must caution
    Thanks Paul. I really should use a hash (not least since my eval string
    gets user input...).
     
    Markus Hänchen, Aug 2, 2006
    #5
  6. Markus Hänchen

    thrill5 Guest

    I think this is what you had in mind:

    $TempOut = 25;
    $name = "TempOut";
    print $$name;

    Scott
     
    thrill5, Aug 4, 2006
    #6
  7. Recommending or showing someone how to use symbolic references without
    pointing out the risks at the same time is quite reckless to say the least.

    jue
     
    Jürgen Exner, Aug 4, 2006
    #7
  8. Markus Hänchen

    perlistpaul Guest

    You shouldn't practice the use of symbolic reference, if you use under
    'strict' it will complain.

    Why not using symbolic reference vs. soft reference.

    You check yourself why not the use it or what reason?

    Phal
     
    perlistpaul, Aug 4, 2006
    #8
  9. Markus Hänchen

    Uri Guttman Guest

    p> You shouldn't practice the use of symbolic reference, if you use under
    p> 'strict' it will complain.

    p> Why not using symbolic reference vs. soft reference.

    those are names for the same thing, so why the vs?

    p> You check yourself why not the use it or what reason?

    obviously english is not your primary language but i can't figure out
    what you mean there.

    uri
     
    Uri Guttman, Aug 4, 2006
    #9
  10. Markus Hänchen

    Anonymous Guest

    This message group is NOT a programming class, it is a forum where people
    can ask questions about Perl and others can HELP them. Just because YOU
    don't think it is good programming practice is no reason not to answer the
    OP's question. Your idea of help is to berate and bully posters, lecture on
    what you think is good practice, what you think is not good practice, how to
    post, read the FAQ, read the docs, etc., etc. This is the most arrogant
    newsgroup on the 'net, and your presence here is the number one contributor
    to that attitude. Quite frankly this newsgroup would be a much better and
    NICER place without you.

    Scott
     
    Anonymous, Aug 5, 2006
    #10
  11. Markus Hänchen

    Uri Guttman Guest

    A> This message group is NOT a programming class, it is a forum where
    A> people can ask questions about Perl and others can HELP them. Just
    A> because YOU don't think it is good programming practice is no
    A> reason not to answer the OP's question. Your idea of help is to
    A> berate and bully posters, lecture on what you think is good
    A> practice, what you think is not good practice, how to post, read
    A> the FAQ, read the docs, etc., etc. This is the most arrogant
    A> newsgroup on the 'net, and your presence here is the number one
    A> contributor to that attitude. Quite frankly this newsgroup would
    A> be a much better and NICER place without you.

    when you start answering all those FAQ's yourself and consistantly do it
    in your nicest way, then we will stop doing the job in our way. as for
    the actual request to use symrefs, if you actually think telling a
    newbie how to do such an evil thing is a good idea, then you must also
    like showing schoolkids how to load and use machine guns. they are about
    the same level of nastiness.

    maybe you don't realize that symrefs are JUST USING THE SYMBOL TABLE AS
    A DATA STRUCTURE. can you see that using a hash for that would be
    faster, safer, cleaner, better in all possible ways? if not, you should
    not be flaming anyone here but i think you know that, don't you? now if
    you go away then this group will become much nicer. in fact much nicer
    than if sinan went away.

    and to top it off, you top posted which is against this groups
    guidelines. you have read those haven't you? they advocate looking for
    answers in the faq.

    and as for what this group is, you have no foundation to say anything
    about that until you have been answering questions on a long term basis
    and others grant you the respect based on your skills. note that trolls
    like robic and moronzilla don't qualify but at least they sometimes
    discuss perl itself unlike your silly useless rant.

    and it is not a forum or a message group. it is a usenet or netnews
    group. but of course you know that since you rule here.

    have a nice coding experience without help from me or most of the
    regulars here. i will make sure to not hire you too.

    yours sincerely,

    a much bigger clpm cop than you will ever be,

    uri
     
    Uri Guttman, Aug 5, 2006
    #11
  12. Markus Hänchen

    perlistpaul Guest

    I give suggestion, but not mean I adhesive with it.
    Symbolic reference is considered dangerous
    Alternative is soft reference


     
    perlistpaul, Aug 7, 2006
    #12
  13. Markus Hänchen

    Paul Lalli Guest

    I have *no* idea what that means. Clearly, English is not your first
    language, and that's fine. But the above is gibberish. "Adhesive"
    means sticky, as in tape or glue.

    Uh. No. "Soft reference" is a synonym for "symbolic reference". Two
    different terms for the same thing.

    Paul Lalli
     
    Paul Lalli, Aug 7, 2006
    #13
  14. Markus Hänchen

    Uri Guttman Guest

    PL> I have *no* idea what that means. Clearly, English is not your first
    PL> language, and that's fine. But the above is gibberish. "Adhesive"
    PL> means sticky, as in tape or glue.


    PL> Yes.

    PL> Uh. No. "Soft reference" is a synonym for "symbolic reference". Two
    PL> different terms for the same thing.

    i told him the same thing earlier in this thread. he doesn't seem to
    read or comprehend our responses.

    uri
     
    Uri Guttman, Aug 7, 2006
    #14
  15. Markus Hänchen

    Anonymous Guest

    Yes, I have read the posting guidelines and I'm top posting anyway, so
    there!

    There is nothing wrong with showing a school kid how to use and load a
    machine gun, just as there is nothing wrong with telling a newbie how to use
    symbolic references. On the other hand, giving a school kid a loaded
    machine gun would be very irresponsible, but what is the worst thing a
    person could do with symrefs? Oh my god, they could crash their
    program!!!!!! And where was it established that the question was posted by
    a newbie and why should I care? I didn't know that certain information was
    only to be disclosed to other members of the elite and I don't remember
    signing a non-disclosure. What a minute, does that mean I haven't been
    inducted to the elite and I'm not suppose to have that information either?
    Is that why you were so incensed that I posted without my e-mail address so
    the thought police, aka clpm cops, can track me down and throw me into the
    gulag? The best way to learn is to try something new and see what happens,
    this is the way newbies turn into experts.

    I have been reading and posting to this newsgroup for many years. The main
    reason I read the newsgroup is to learn more about Perl, and reading
    questions and answers in the newsgroup furthers that. I don't post often
    because:
    a) I have a real job and a life and I only peruse the newsgroups every few
    days, and sometimes not for weeks.
    b) If I don't have anything CONSTRUCTIVE to add, I don't post.

    Berating and bulling posters discourages others from posting questions.
    Stupidity is in the eye of the beholder, but then again I'm sure people go
    out of their way to post stupid questions so they can be belittled by the
    likes of you an Sinan.

    I have never posted a question here, and probably never will. I know how to
    read the FAQs, and know how to read a book, and just about everything you
    need to know about Perl is published somewhere. The questions I would ask,
    I would be told to read the FAQ anyway, so what's the point? Yeah, the
    amount of time it would take to answer the question would the same or less
    than telling me to read the FAQ, but I would be told that anyway. I would
    LIKE to be able to post simple questions because someone here could probably
    answer it in 2 minutes or less, while it might take me much longer than that
    to track down the correct documentation to read. It would be a tragedy to
    post an answer to a question that someone could read in the documentation.

    The posting guidelines are just that GUIDELINES. There are no rules here.
    If you want rules and want them followed, go troll comp.lang.perl.moderated.

    I don't currently work as a professional programmer and don't have any plans
    to do so, but it still hurts that you wouldn't recommend me. I'm so hurt
    I'm seriously considering rewriting my current Perl project to use symrefs
    and killing my program. You have also put me in my place and proven to me I
    don't rule here because I called this a "forum and newsgroup" instead of "a
    usenet or netnews". You really do RULE here big comp.lang.perl.misc cop
    dude!!!!!

    Making you sound stupid sure is a lot of fun, maybe there is something to
    being a Usenet bully. It's not very constructive, or nice and I once read
    "it's nice to be important, but it's more important to be nice." I wonder
    where I could have read that....

    Scott

    Ooopps, I mean "anonymous"
     
    Anonymous, Aug 9, 2006
    #15
  16. Markus Hänchen

    anno4000 Guest

    So long then

    Anno
     
    anno4000, Aug 9, 2006
    #16
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Similar Threads
There are no similar threads yet.
Loading...