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


In reply to Re: pushing a $variable into an array with varible name by davido
in thread pushing a $variable into an array with varible name by han_perl

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.