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

This there anyway to push a variable into an varible array name?? I'm trying to do this but it just goes into the same array.

============= Program ===================== @vol = ( "vol1", "vol2" ); $contextID = 9; $datastream = "this is just a data stream test"; ( $a, $b, $c, $d, $e, $f, $g ) = split /[\s]+/, $datastream; push @{test-$contextID-$vol[0]}, $a; push @{test-$contextID-$vol[0]}, $b; print "output0: @{test-$contextID-$vol[0]}\n"; push @{test-$contextID-$vol[1]}, $a; print "output1: @{test-$contextID-$vol[1]}\n"; ========= Output ===================== $ perl 1.pl output0: this is output1: this is this
The output1 should just be "this", not "this is this".

Thanks.

Replies are listed 'Best First'.
Re: pushing a $variable into an array with varible name
by thor (Priest) on Jun 17, 2004 at 21:08 UTC
    It looks like you might want a hash of arrays.
    @vol = ( "vol1", "vol2" ); $contextID = 9; $datastream = "this is just a data stream test"; ( $a, $b, $c, $d, $e, $f, $g ) = split /\s+/, $datastream; my %hash; $"=" "; #see 'perldoc perlvar' for what this means push @{$hash{"$contextID-$vol[0]"}}, $a; push @{$hash{"$contextID-$vol[0]"}}, $b; print "output0: @{$hash{"$contextID-$vol[0]"}}\n"; push @{$hash{"$contextID-$vol[1]"}}, $a; print "output1: @{{"$contextID-$vol[1]"}}\n";

    thor

    Update: Added double-quotes to hash indicies as per request below
      You need double-quotes around those hash subscripts.

      We're not really tightening our belts, it just feels that way because we're getting fatter.
Re: pushing a $variable into an array with varible name
by davido (Cardinal) on Jun 18, 2004 at 00:34 UTC

    At the top of your script you should put use warnings;. Then when you run it, you'll find out a lot about why your solution isn't working. The immediate problem is the unquoted '-' operators... you want them to be seen as a textual part of the variable name, but Perl sees them as a subtraction operator. Here is your code, modified in a way that generates fewer (but still some) warnings and the correct output:

    use warnings; @vol = ( "vol1", "vol2" ); $contextID = 9; $datastream = "this is just a data stream test"; ( $a, $b, $c, $d, $e, $f, $g ) = split /[\s]+/, $datastream; push @{'test-' . $contextID . '-' . $vol[0]}, $a; push @{'test-' . $contextID . '-' . $vol[0]}, $b; print "output0: @{'test-' . $contextID . '-' . $vol[0]}\n"; push @{'test-' . $contextID . '-' . $vol[1]}, $a; print "output1: @{'test-' . $contextID . '-' . $vol[1]}\n";

    Now the output is:

    output0: this is output1: this

    However, this is still a flawed solution. Now at the top of your script put (right above the use warnings; line) use strict;. Leave that there and run the script. ...it won't run. Why? Because you're using symbolic references. Most programming languages won't even let you do that, and yet they manage just fine without symbolic refs. You should probably be using a hash instead, or some other datastructure, but avoid symbolic references until you know enough to know why not to use them. Read the following offsite links for a better understanding of the ills of using symbolic references: http://perl.plover.com/varvarname.html, http://perl.plover.com/varvarname2.html, and http://perl.plover.com/varvarname3.html.


    Dave

Re: pushing a $variable into an array with varible name
by amw1 (Friar) on Jun 17, 2004 at 21:08 UTC
    I would uses hashes.
    push @{$hash->{"test-" . $contextID . "-" . $vol0}}, $a
    more info can be found here.
Re: pushing a $variable into an array with varible name
by Ido (Hermit) on Jun 18, 2004 at 00:15 UTC
    You are trying to use symbolic refs. This is usually a bad idea (and you should always use strict to avoid it...), but in case you know what you're doing and insist:
    the expression:
    test-$number-$string Is like  'test' - $string - $number
    So, in all of the three push statements, you're pushing to the array @{-9} (strings are like 0 in numeral context, both 'vol1' and 'vol2', and the - operator implies numberal context).
    What you'd probably want to do is use them as strings, such as:
    @{"test-$contextID-$vol[0]"}