rwitmer has asked for the wisdom of the Perl Monks concerning the following question:

I want to make a hash of arrays. I thought I was doing it correctly, but apparently I'm not. I expect a result like this:
10 a l 11 b m
Here's the code:
#!/usr/bin/perl use strict; use warnings; my $filename = $ARGV[0]; my $outFilename = $ARGV[1]; open OUTFILE, ">", $outFilename or die $!; open FILE, $filename or die $!; my $id; my $date; while(<FILE>) { my $line = $_; if($_ =~ /(.*?)\t+(.*?)\[/ ) { $id = $1; $date = $2; print OUTFILE "$id\t$date\n"; } my $key; my $i; my %HoA; push @{ $HoA{$id} }, $date; my $k; foreach $k (keys %HoA) { print "$k\n\t"; foreach (@{$HoA{$k}}) { print " $_"; } print "\n"; } }
The input is like this:
10 a[ 11 b[ 12 c[ 13 d[ 14 e[ 15 f[ 16 g[ 17 h[ 18 i[ 19 j[ 20 k[ 10 l[ 11 m[ 12 n[ 13 o[ 14 p[ 15 q[ 16 r[ 17 s[ 18 t[ 19 y[ 20 v[
But the output is sadly like this:
10 a 11 b 12 c 13 d 14 e 15 f 16 g 17 h 18 i 19 j 20 k 10 l 11 m 12 n 13 o 14 p 15 q 16 r 17 s 18 t 19 y 20 v
This confuses me because it looks like it is making more than one hash entry per key. What am I doing wrong here?

Replies are listed 'Best First'.
Re: Hash of Arrays
by wind (Priest) on Mar 09, 2011 at 21:50 UTC

    Your %HoA data structure is defined within your processing loop. Try running your code through Perl::Tidy to see some of the obvious structural problems.

    Just do the processing like demonstrated below. You can change it back to file processing on your own:

    #!/usr/bin/perl use strict; use warnings; my %HoA; while (<DATA>) { if (/(\d+)\s+(.*?)\[/) { push @{$HoA{$1}}, $2; } else { warn "Unknown data format: $_\n"; } } foreach my $k (sort keys %HoA) { print "$k\n\t@{$HoA{$k}}\n"; } __DATA__ 10 a[ 11 b[ 12 c[ 13 d[ 14 e[ 15 f[ 16 g[ 17 h[ 18 i[ 19 j[ 20 k[ 10 l[ 11 m[ 12 n[ 13 o[ 14 p[ 15 q[ 16 r[ 17 s[ 18 t[ 19 y[ 20 v[
    Outputs:
    10 a l 11 b m 12 c n 13 d o 14 e p 15 f q 16 g r 17 h s 18 i t 19 j y 20 k v
Re: Hash of Arrays
by fidesachates (Monk) on Mar 09, 2011 at 22:02 UTC
    Wind pretty much solved the problem correctly. All I have to add is don't forget to close the files you opened.