doubt regarding Symbolic references

Discussion in 'Perl Misc' started by Rajesh, Mar 8, 2005.

  1. Rajesh

    Rajesh Guest

    Lets Consider a simple program like this

    $i = 'a';
    $a = "why is it so boring?";
    print "$$i";

    Now, the output will be the string "why is it so boring?" because $$i
    will now get referenced to $a and hence the string.

    Now, instead $i has the value $a . Something like,

    $i = '$a';
    $a = "why is it so boring?";
    print "$i";

    In this case, the output will be "$a" and not the string that is
    expected.
    But, I want the output to be the string. How would I get the value of a
    variable if another variable has this variable?
    Rajesh, Mar 8, 2005
    #1
    1. Advertising

  2. Rajesh

    Brian Wakem Guest

    Rajesh wrote:
    > Lets Consider a simple program like this
    >
    > $i = 'a';
    > $a = "why is it so boring?";
    > print "$$i";
    >
    > Now, the output will be the string "why is it so boring?" because $$i
    > will now get referenced to $a and hence the string.
    >
    > Now, instead $i has the value $a . Something like,
    >
    > $i = '$a';
    > $a = "why is it so boring?";
    > print "$i";
    >
    > In this case, the output will be "$a" and not the string that is
    > expected.
    > But, I want the output to be the string. How would I get the value of a
    > variable if another variable has this variable?



    You could use:-

    print eval $i;


    --
    Brian Wakem
    Brian Wakem, Mar 8, 2005
    #2
    1. Advertising

  3. Rajesh

    Paul Lalli Guest

    "Rajesh" <> wrote in message
    news:...
    > Lets Consider a simple program like this
    >
    > $i = 'a';
    > $a = "why is it so boring?";
    > print "$$i";


    please read:
    perldoc -q quoting
    What's wrong with always quoting $vars.

    >
    > Now, the output will be the string "why is it so boring?" because $$i
    > will now get referenced to $a and hence the string.
    >
    > Now, instead $i has the value $a . Something like,
    >
    > $i = '$a';
    > $a = "why is it so boring?";
    > print "$i";
    >
    > In this case, the output will be "$a" and not the string that is
    > expected.
    > But, I want the output to be the string. How would I get the value of

    a
    > variable if another variable has this variable?


    Another responder already told you about eval(). I will give you the
    standard warning - YOU DON'T WANT TO DO THIS! Symbolic references are
    bad, evil, and should not be used unless absolutely necessary. They
    almost never are. The correct answer is to use a hash:

    $i = 'a';
    $vars{a} = 'Why is it so boring?';
    print $vars{$i};

    For a more thorough discussion on why you should not be using symrefs,
    please consult the FAQ:

    perldoc -q "variable name"

    Paul Lalli
    Paul Lalli, Mar 8, 2005
    #3
  4. Rajesh wrote:

    > Lets Consider a simple program like this
    >
    > $i = 'a';
    > $a = "why is it so boring?";
    > print "$$i";
    >
    > Now, the output will be the string "why is it so boring?" because $$i
    > will now get referenced to $a and hence the string.


    Yes, it will. Don't do that.
    >
    > Now, instead $i has the value $a . Something like,
    >
    > $i = '$a';
    > $a = "why is it so boring?";
    > print "$i";
    >
    > In this case, the output will be "$a" and not the string that is
    > expected.
    > But, I want the output to be the string. How would I get the value of a
    > variable if another variable has this variable?


    Don't. Use a hash.

    --
    Christopher Mattern

    "Which one you figure tracked us?"
    "The ugly one, sir."
    "...Could you be more specific?"
    Chris Mattern, Mar 8, 2005
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Bob Nelson

    doubt about doubt

    Bob Nelson, Jul 28, 2006, in forum: C Programming
    Replies:
    11
    Views:
    604
  2. mike

    can't create symbolic references

    mike, Mar 26, 2006, in forum: Perl Misc
    Replies:
    1
    Views:
    68
    Matt Garrish
    Mar 26, 2006
  3. jhumanski
    Replies:
    15
    Views:
    728
    Rainer Weikusat
    Apr 24, 2012
  4. Replies:
    0
    Views:
    543
  5. Peter Otten
    Replies:
    2
    Views:
    104
    Cousin Stanley
    Aug 10, 2013
Loading...

Share This Page