bangers has asked for the wisdom of the Perl Monks concerning the following question:

First of all let me say that I think this is a bad idea, but I was asked the question and I'm now curious to know the answer.

If I have a set of variables $a, $b, $c…etc

If a different variable – call it $want - contains the value 'a' I want to return the contents of $a, if it contains 'b' I want to return the contents of $b etc.

I know better ways to structure this e.g. with hashes, but I am curious as to how to get a value of a scalar specified by a second scalar. the best I've done so far is:
#!/usr/bin/perl -w my $client = "Yes"; my $want = "client"; print *{$want},"\n";
Any ideas?

Replies are listed 'Best First'.
Re: Value of a scalar specified by a second scalar
by davido (Cardinal) on May 17, 2006 at 17:28 UTC

    You can't do it with lexical (my) variables. Symbolic references only work with variables that reside in the symbol table (which lexicals don't). Here's an example:

    ( $a, $b, $c ) = ( 10, 20, 30 ); $want = 'a'; print $$want, "\n";

    In your eample, you're treating lexicals as if they have symbolic entries in the global symbol table. They just don't. So when you print *{$want}, you're not actually accessing the lexical, $client.


    Dave

      Ah. simple when you know how. I had tried $$want, but as I always use lexical variables is hadn't worked. Thanks
Re: Value of a scalar specified by a second scalar
by Zaxo (Archbishop) on May 17, 2006 at 17:30 UTC

    You're talking about symbolic references. The syntax you are looking for is just like that for real references.

    $c = 'foo'; $want = 'c'; print $$want, $/;
    Strictures will complain about symrefs and fail to run. As you say, usually a hash is wanted when they are introduced.

    After Compline,
    Zaxo

      Is there any reason or benefit to prefering
      print $$want, $/;
      over
      print $$want, "\n";
      I find myself tacking "\n" on the end of prints more often then I think I should.
      Just wondering if there's a better way.

        Only minor advantages. It's two fewer keystrokes and it makes output conform to the current input record seperator, which is sometimes good to ensure.

        Mainly, it's one of my coding habits. I formed it under the superstition that it's more platform neutral. That's not really true, but the minor advantages win for a habit I've already got.

        The truly handy way to stop fussing with newlines is to set local $\ = "\n"; and know that all distinct prints in that scope will get newlines appended.

        $ perl -e'local $\ = $/; print for qw/foo bar baz/' foo bar baz $

        After Compline,
        Zaxo

Re: Value of a scalar specified by a second scalar
by VSarkiss (Monsignor) on May 17, 2006 at 17:28 UTC
      The code (not mine) is badly structured and has other problems so I will get it changed. But ignorance is always dumber than knowledge and I didn't know the answer to the question I was asked. Now I do. So no the question wasn't stupid and as you now know the answer too, you too are a little less dumb then you were.
Re: Value of a scalar specified by a second scalar
by TedPride (Priest) on May 17, 2006 at 19:46 UTC
    It's just as easy to put a lot of things in a hash:
    use strict; use warnings; my (%hash, $want); @hash{qw/a b c/} = (1, 2, 3); $want = 'c'; print $hash{$want};
    or
    use strict; use warnings; my (%hash, @keys, @vals, $want); @keys = qw/a b c/; @vals = (1, 2, 3); @hash{@keys} = (@vals); $want = 'c'; print $hash{$want};