Here's a more detailed response than you've received yet.

Any time you find yourself saying "I have the name of a variable in a string, and I want the value in that variable", stop.

You're programming like a Perl 4 programmer. You are using the symbol table to build your data structures. Back in those days you couldn't have an array of hashes. But you could have an array containing the names of hashes. Here you're building a data structure that's a hash whose values are arrays. You're using the symbol table as the top-level hash.

The solution is to use Perl 5 programming techniques, namely references. You can actually have a hash of arrays, so when you look up a string like "alpha" or "num" you get back a reference to an array.

Your code would then look like:

foreach $var (@vars) {
  push @{ $myhash{$var} }, $var;
}
It looks like you're reading a file a record at a time. Each record contains a bunch of fields. You want an array for each field, so that $name[0] is the name of the 0th person, $age[0] is the age of the 0th person, etc.

Each record you read, you split into $name, $age, and so on. Then you need some magic to push $name onto @name, $age onto @age, etc.

There is two solutions to this problem. The first is to split into a hash: $person{name}, $person{age}, etc. Then you can do:

foreach $field (keys %person) {
  push @{ $myhash{$field} }, $person{$field};
}
A better approach, depending on what you're going to do with the data, might be to have an array of records. Then for each record, you simply do:

  my %record;
  # populate $record{NAME}, $record{AGE}, etc
  push @records, \%record;
Now if you want to talk about the 5th person's name, say:
$records[5]{NAME}
Definitely learn about references, though. The latest versions of Perl come with MjD's excellent perlreftut manpage, which is a great place to start.

In reply to RE: push with variable substitution ? by gnat
in thread push with variable substitution ? by Anonymous Monk

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.