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. |