in reply to pushing a $variable into an array with varible name
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
|
|---|