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

greeting holy ones !

Can I define a new variable with a name that stored in another variable?
for example:
# if I have: $name = 'newVar'; I would like somehow to have a new variable $newVar
Actually, on runtime I have a regexp, something like:
$tmp =~ /(.+)(\d+)$/;
and I want to store something in an array by the name stored in $1 (from the regexp) under the index $2 (from the regexp too), the thing is that if the array doesn't already exist, I want to create it on the spot (and initialize it's $2 index). B.T.W. I don't have the names of the arrays in advance, otherwise I would have predefined them.

Hotshot

Replies are listed 'Best First'.
Re: defining a new var on the spot
by davorg (Chancellor) on Dec 11, 2001 at 18:33 UTC

    Yes you can do it. Look up "symbolic references" in any decent Perl book and you'll see an explaination of how to do it - right next to an explaination of why it's such a bad idea. For a good explaination of why you shouldn't do it see these three articles.

    It's such a bad idea that if you run your programs under use strict (as everyone should do) then symbolic references are explicity flagged as an error.

    A much better idea would be to create a hash or arrays (perhaps call it %arrays where you can store these values.

    my %arrays; $tmp =~ /(.+)(\d+)$/; $arrays{$1}->[$2] = 'some value';

    Oh, and this is a very frequently asked question round here. I find it impossible to believe that Super Search wouldn't have shown you many nodes all saying the same thing.

    --
    <http://www.dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

Re: defining a new var on the spot
by merlyn (Sage) on Dec 11, 2001 at 21:51 UTC
    Can I define a new variable with a name that stored in another variable?
    I always read this as "Can I poke myself in the eye with a sharp stick?". Yes, you can.

    If the answer is "define a new variable with a name defined by another variable", you've asked the wrong question. Back up a step. What's the problem you're really solving?

    -- Randal L. Schwartz, Perl hacker

Re: defining a new var on the spot
by MZSanford (Curate) on Dec 11, 2001 at 18:30 UTC
    You need SymRefs. Somthing like :
    $tmp =~ m/(.+)(\d+)$/; ${$1} = $2;

    But, There are great many nodes in Super Search which i suggest reading, as this is usually a poor coding practice. If you read the nodes, and feel this is still needed, then the above would be the way to do it.
    $ perl -e 'do() || ! do() ;' Undefined subroutine &main::try
Re: defining a new var on the spot
by mirod (Canon) on Dec 11, 2001 at 18:43 UTC

    This question has been asked so many times... ;-(

    You don't want to do this. It is dangerous. See Dominus's expalnations on Why it's stupid to 'use a variable as a variable name'.

    If you don't know the name of the array, then use a hash:

    #!/bin/perl -w use strict; my %tmp; while( my $tmp=<DATA>) { if( $tmp=~ /^(.+)(\d+)$/) { $tmp{$1}->[$2]= 'foo'; # don't you love auto-vivification? } } __DATA__ foo2 foo5 foo0 bar bar2 bar1 foo3
Re: defining a new var on the spot
by andye (Curate) on Dec 11, 2001 at 18:42 UTC
    Hotshot, others have explained why using symbolic references is a bad idea, here's an alternative:
    my %hash_of_arrays; $tmp =~ /(.+)(\d+)$/; $hash_of_arrays{$1}->[$2] = $the_thing_to_store ; use Data::Dumper; print Dumper(\%hash_of_arrays);
    hth, andy.

    Update: dammit, davorg's already said that! I plead snow blindness.

Re: defining a new var on the spot
by strat (Canon) on Dec 11, 2001 at 18:33 UTC
    You could try to do this with eval:
    $name = 'anyVarName'; eval ("\$$name = 20;"); print $anyVarName;

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"