The code you listed does in fact work, provided that you don't have "use strict;" in you your program - so if it does not do what you want, the problem may be with the input data or with other parts of your program.

For example if @files is defined as...

my @files = ("foo.txt");

And the contents of file foo.txt are (PS: that whitespace is supposed to be tabs)...

aaa bbb ccc 000 111 222

...then at the end of your code, the hash %wow0 will contain:

( "111" => ["sth"], "bbb" => ["sth"] )

However, using symbolic references for that is most likely a bad idea - they tend to make your program fragile with regards to unexpected input, and make it easy to introduce annoying bugs. That's why "use strict;" disables symbolic references.

As runrig already mentioned, you should consider using a data structure that does not rely on symbolic references, like a single nested hash or array that contains what you currently try to store in multiple %wow0, %wow1, ... hashes.

For example:

use warnings; use strict; my @files = ("foo.txt", "bar.txt"); my @wow; foreach my $i (0..$#files) { open my $fh, "<", $files[$i]; while(<$fh>){ chomp; my @row = split /\t/; push @{$wow[$i]->{$row[1]}}, "sth"; } close $fh; }

Now $wow[0] (i.e. the first element of the array @wow) will contain a reference to an anonymous hash that corresponds to what you wanted to store in %wow0, and $wow[1] corresponds to your %wow1, etc., so that the contents of @wow will look like:

( { "111" => ["sth"], # data from foo.txt "bbb" => ["sth"] }, { ... } # data from bar.txt )

Of course that's just one possibility. What data structure is best, depends on what exactly you're trying to do with that code. If you give more info (ideally including some sample input & expected output), we can help you better.


In reply to Re: How to correctly use a symbolic reference to automatically generate hash names? by smls
in thread How to correctly use a symbolic reference to automatically generate hash names? by lightoverhead

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.