${$key} or similar

Discussion in 'Perl Misc' started by Marten Lehmann, Oct 20, 2008.

  1. Hello,

    I'm using "use strict" in the beginning of every script, but for one
    certain case, I want to access variables by their names like this:

    foreach $type ("billing", "admin", "tech") {
    print ${$type};
    }

    The value shall be the same is if I would call $billing, $admin or
    $tech. I can assure that these three variables exist in my script and
    although I could maybe rewrite parts if the script to put the values in
    a hash, I really would like to know how can I access them without a
    hash. Any ideas?

    Regards
    Marten
    Marten Lehmann, Oct 20, 2008
    #1
    1. Advertising

  2. Marten Lehmann

    Guest

    Marten Lehmann <> wrote:
    > Hello,
    >
    > I'm using "use strict" in the beginning of every script, but for one
    > certain case, I want to access variables by their names like this:
    >
    > foreach $type ("billing", "admin", "tech") {
    > print ${$type};
    > }


    foreach $type (\$billing, \$admin, \$tech) {
    print ${$type};
    }


    > The value shall be the same is if I would call $billing, $admin or
    > $tech. I can assure that these three variables exist in my script and
    > although I could maybe rewrite parts if the script to put the values in
    > a hash, I really would like to know how can I access them without a
    > hash. Any ideas?


    You could temporarily turn off strictures, or use fully qualified variable
    names. But you would have to use them everywhere for those variables, not
    just in the foreach loop.

    --
    -------------------- http://NewsReader.Com/ --------------------
    The costs of publication of this article were defrayed in part by the
    payment of page charges. This article must therefore be hereby marked
    advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
    this fact.
    , Oct 20, 2008
    #2
    1. Advertising

  3. Hello,

    >> foreach $type ("billing", "admin", "tech") {
    >> print ${$type};
    >> }

    >
    > foreach $type (\$billing, \$admin, \$tech) {
    > print ${$type};
    > }


    I cannot change it, I need to work with strings because I need to print
    them also, e.g.

    print "$type: ". ${$type}. "\n";

    > You could temporarily turn off strictures, or use fully qualified variable
    > names. But you would have to use them everywhere for those variables, not
    > just in the foreach loop.


    Turning strict temporarily off omits the errors, but neither ${$type}
    nor $$type returns the expected value. How do I have to write it correctly?

    Regards
    Marten
    Marten Lehmann, Oct 20, 2008
    #3
  4. Marten Lehmann wrote:
    >
    > I'm using "use strict" in the beginning of every script, but for one
    > certain case, I want to access variables by their names like this:
    >
    > foreach $type ("billing", "admin", "tech") {
    > print ${$type};
    > }
    >
    > The value shall be the same is if I would call $billing, $admin or
    > $tech. I can assure that these three variables exist in my script and
    > although I could maybe rewrite parts if the script to put the values in
    > a hash, I really would like to know how can I access them without a
    > hash. Any ideas?


    What you are attempting to do *is* access them through a hash, it's just
    not a user defined hash. See the "Symbol Tables" section of perlmod.pod.



    John
    --
    Perl isn't a toolbox, but a small machine shop where you
    can special-order certain sorts of tools at low cost and
    in short order. -- Larry Wall
    John W. Krahn, Oct 21, 2008
    #4
  5. Marten Lehmann

    Jim Gibson Guest

    In article <>, Marten Lehmann
    <> wrote:

    > Hello,
    >
    > >> foreach $type ("billing", "admin", "tech") {
    > >> print ${$type};
    > >> }

    > >
    > > foreach $type (\$billing, \$admin, \$tech) {
    > > print ${$type};
    > > }

    >
    > I cannot change it, I need to work with strings because I need to print
    > them also, e.g.
    >
    > print "$type: ". ${$type}. "\n";
    >
    > > You could temporarily turn off strictures, or use fully qualified variable
    > > names. But you would have to use them everywhere for those variables, not
    > > just in the foreach loop.

    >
    > Turning strict temporarily off omits the errors, but neither ${$type}
    > nor $$type returns the expected value. How do I have to write it correctly?


    It does on my system:

    % perl -e '$type=q(tech);$tech=q(tech value);print "$type: ${$type}\n";'
    tech: tech value

    Please post a short program that shows what you are getting on your
    system.

    To write it correctly, you need to use a hash. Why do you not wish to
    use a hash?

    --
    Jim Gibson
    Jim Gibson, Oct 21, 2008
    #5
  6. Marten Lehmann

    C.DeRykus Guest

    On Oct 20, 3:42 pm, Marten Lehmann <> wrote:
    > Hello,
    >
    > >> foreach $type ("billing", "admin", "tech") {
    > >> print ${$type};
    > >> }

    >
    > > foreach $type (\$billing, \$admin, \$tech) {
    > > print ${$type};
    > > }

    >
    > I cannot change it, I need to work with strings because I need to print
    > them also, e.g.
    >
    > print "$type: ". ${$type}. "\n";
    >
    > > You could temporarily turn off strictures, or use fully qualified variable
    > > names. But you would have to use them everywhere for those variables, not
    > > just in the foreach loop.

    >
    > Turning strict temporarily off omits the errors, but neither ${$type}
    > nor $$type returns the expected value. How do I have to write it correctly?
    >


    Not recommended for queasy stomachs but you could use string eval:

    my $code = 'print ';
    for ( qw/billing admin tech/) {
    $code .= ('$' . __PACKAGE__
    . '::' . "$_,"
    );
    eval $code; die $? if $?;
    C.DeRykus, Oct 21, 2008
    #6
  7. Marten Lehmann

    Guest

    On Tue, 21 Oct 2008 00:42:28 +0200, Marten Lehmann <> wrote:

    >Hello,
    >
    >>> foreach $type ("billing", "admin", "tech") {
    >>> print ${$type};
    >>> }

    >>
    >> foreach $type (\$billing, \$admin, \$tech) {
    >> print ${$type};
    >> }

    >
    >I cannot change it, I need to work with strings because I need to print
    >them also, e.g.
    >
    >print "$type: ". ${$type}. "\n";
    >
    >> You could temporarily turn off strictures, or use fully qualified variable
    >> names. But you would have to use them everywhere for those variables, not
    >> just in the foreach loop.

    >
    >Turning strict temporarily off omits the errors, but neither ${$type}
    >nor $$type returns the expected value. How do I have to write it correctly?
    >
    >Regards
    >Marten


    I don't know why you need to do this, or why you don't prefer a hash key
    and value approach, but this does it what you want.

    sln

    ---------------
    use strict;
    use warnings;

    my ($billing, $admin, $tech) = ('b','a','t');

    foreach my $type ("billing", "admin", "tech") {
    eval "print \"$type = \$$type\n\"";
    }

    __END__

    output:

    billing = b
    admin = a
    tech = t
    , Oct 21, 2008
    #7
  8. Marten Lehmann wrote:
    > Turning strict temporarily off omits the errors, but neither ${$type}
    > nor $$type returns the expected value. How do I have to write it
    > correctly?


    Symbolic references only work with global variables. You can't use them
    with lexical (my) variables.

    -mjc
    Michael Carman, Oct 21, 2008
    #8
  9. Marten Lehmann wrote:
    > I'm using "use strict" in the beginning of every script, but for one
    > certain case, I want to access variables by their names like this:
    >
    > foreach $type ("billing", "admin", "tech") {
    > print ${$type};
    > }
    >
    > The value shall be the same is if I would call $billing, $admin or
    > $tech. I can assure that these three variables exist in my script and
    > although I could maybe rewrite parts if the script to put the values in
    > a hash, I really would like to know how can I access them without a
    > hash. Any ideas?


    I'm guessing that you want to avoid a hash because you want to minimize
    changes to the rest of your program. How about a hash of references instead?

    my %types = (
    billing => \$billing,
    admin => \$admin,
    tech => \$tech,
    );

    while (my ($type, $ref) = each %types) {
    print "$type: $$ref\n";
    }

    If the order is important, you could use a LoL (list of lists):

    my @types = (
    ['billing' => \$billing],
    ['admin' => \$admin],
    ['tech' => \$tech],
    );

    foreach my $t (@types) {
    print "$t->[0]: ${$t->[1]}\n";
    }

    -mjc
    Michael Carman, Oct 21, 2008
    #9
  10. Marten Lehmann

    Guest

    On Tue, 21 Oct 2008 00:19:23 GMT, wrote:

    >On Tue, 21 Oct 2008 00:42:28 +0200, Marten Lehmann <> wrote:
    >
    >>Hello,
    >>
    >>>> foreach $type ("billing", "admin", "tech") {
    >>>> print ${$type};
    >>>> }
    >>>
    >>> foreach $type (\$billing, \$admin, \$tech) {
    >>> print ${$type};
    >>> }

    >>
    >>I cannot change it, I need to work with strings because I need to print
    >>them also, e.g.
    >>
    >>print "$type: ". ${$type}. "\n";
    >>
    >>> You could temporarily turn off strictures, or use fully qualified variable
    >>> names. But you would have to use them everywhere for those variables, not
    >>> just in the foreach loop.

    >>
    >>Turning strict temporarily off omits the errors, but neither ${$type}
    >>nor $$type returns the expected value. How do I have to write it correctly?
    >>
    >>Regards
    >>Marten

    >
    >I don't know why you need to do this, or why you don't prefer a hash key
    >and value approach, but this does it what you want.
    >
    >sln
    >
    >---------------
    >use strict;
    >use warnings;
    >
    >my ($billing, $admin, $tech) = ('b','a','t');
    >
    >foreach my $type ("billing", "admin", "tech") {
    > eval "print \"$type = \$$type\n\"";


    ^^^^^^^^^^^^^^^^^^^^^^^
    or
    eval 'print ' . '"' . $type . ' = ' . '$' . $type . '\n' . '"';

    Might be an easier way of looking at it. Look up eval and see what it does (dynamic code?).

    --------------

    (more of the same)

    my @names = qw(billing admin tech);

    for ( @names ) {
    eval 'print ' . '"' . $_ . ' = ' . '$' . $_ . '\n"';
    }


    Good luck

    sln
    , Oct 21, 2008
    #10
    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. bjam
    Replies:
    0
    Views:
    386
  2. Robert Oschler
    Replies:
    5
    Views:
    602
    Jonathan Bartlett
    Apr 11, 2005
  3. M P
    Replies:
    1
    Views:
    449
  4. Ben Bacarisse
    Replies:
    8
    Views:
    337
    Ben Bacarisse
    Dec 22, 2011
  5. 88888 Dihedral
    Replies:
    35
    Views:
    1,102
    88888 Dihedral
    Jan 4, 2012
Loading...

Share This Page