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

Example
$var1 = 7; $var2 = 3; foreach $number(1..2) { print $var${number} . "\n"; }
this doesn't work....docos are bare on this....how to construct variable names on the fly?

Replies are listed 'Best First'.
Re: construct variable name on the fly
by Fletch (Bishop) on Oct 19, 2004 at 00:44 UTC

    They don't really mention it because it's a bad idea. In your case you want an array @var; in the more general class you'd use a hash. See perldoc perlref for discussion about symbolic references.

    Update: See also Program Repair Shop and Red Flags (and while not directly relevant there's another two Red Flag articles here and here that are well worth checking out).

      i know it's a bad idea.....and i will rectify when i have time. right now i have a report overdue (work) and need to get it going. Any help on getting the keys from a hash whose name is constructed from other variables would be appreciated. e.g. keys %this.${somevar}.name; doesnt work.
Re: construct variable name on the fly
by Juerd (Abbot) on Oct 19, 2004 at 20:44 UTC
Re: construct variable name on the fly
by nedals (Deacon) on Oct 19, 2004 at 06:49 UTC
    In direct response... but it will not work under 'strict'
    $var1 = 7; $var2 = 3; foreach $number(1..2) { $varname = 'var'.$number; print $$varname."\n"; }
    Or you could use a hash
    use strict; my %var; $var{1} = 7; $var{2} = 3; foreach my $number(1..2) { print $var{$number}."\n"; }
Re: construct variable name on the fly
by TedPride (Priest) on Oct 19, 2004 at 14:04 UTC
    print ${"var$_"}."\n" for (1..2);

    ${'var1'} gives the same results as $var1.

Re: construct variable name on the fly
by aquarium (Curate) on Oct 19, 2004 at 01:11 UTC
    thank you....the dot operator did the trick. this doesn't work for getting keys from such a constructed variable though. Any help in this regard please. Example:
    $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys %.$hashname;
      $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);
      A reply falls below the community's threshold of quality. You may see it by logging in.
Re: construct variable name on the fly
by bradcathey (Prior) on Oct 19, 2004 at 00:39 UTC

    Here's a method, but it doesn't work under strict.

    { no strict 'subs'; foreach $number(1..2) { print $var.$number . "\n"; } }

    Update: Oops, a little too fast on the draw. I confused it with populating HTML::Template params. Way off!

    { no strict 'subs'; foreach $number(1..2) { $template ->param( var.$number => $somevalue ); } }

    So, Fletch is absolutely correct on both accounts.


    —Brad
    "Don't ever take a fence down until you know the reason it was put up." G. K. Chesterton

      Whatever you posted before your update, or after, why no strict 'subs' has anything to do with the problem.What you want to mention is really 'refs'.

      This works:

      $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);

      This works:

      use strict('subs'); $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);

      This does not work:

      use strict('refs'); $thisname{'frank'} = 1; $thisname{'lee'} = 2; $hashname = 'thisname'; print keys(%$hashname);

      No, that doesn't work even under strict. ${ "var$number" } would, but again symbolic references are bad practice.

A reply falls below the community's threshold of quality. You may see it by logging in.