in reply to hash of hashes?
This seems to do what you want (although your specifications are a bit vague, at least, so I followed your description as close to the letter as I could). Note that I wrote this up while tired (worked over 9 hours during a hectic night at the restaurant) and inebriated (had a couple of drinks afterwards) so it can't be that hard :) Then again, I admit this may not be the most elegant way of going about it, but it does what you want and it should get you on the right track. Even if it's an XY problem.
use strict; use warnings; my $file1 = << "EOF"; # I'm too lazy to create the actual files... >A Amber, Amy >B Barbie, Bambie EOF my $file2 = << "EOF"; # So I'll just define them as heredocs hre... >A Austin, Aurora >C Cathy, Candy EOF my $file3 = << "EOF"; # and read from them in the next part of the c +ode. >B Bob, Barbara >C Cane, Carter EOF my @files = (\$file1, \$file2, \$file3); # References to the scalar +s above. my %names = (); # Oh, you know... a hash. for my $f (@files) { # Iterate through the refe +rences open my $fh, "<", $f; # "Open" them for reading. my $key = ""; # We don't have a key yet. my %tmp_names = (); # Just a temporary hash... $tmp_names{$_} = ["-","-"] for qw(A B C); # ... for the blanks. while (<$fh>) { # Read lines. chomp; # I don't care about newli +nes. if (m/^>([ABC])$/) { # Is it ">" + A, B, or C? $key = $1; # Then it's a key for the +hash. next; } elsif ($key # Do we have a key yet? && $_) { # And it isn't a blank lin +e? my @names = split(/, /, $_); # Then it's a list of name +s! $tmp_names{$key} = \@names; # Store them in the tempor +ary hash } } close $fh; for (qw(A B C)) { push @{$names{$_}}, @{$tmp_names{$_}}; # Move tmp hash to real + hash. } } for (qw(A B C)) { print ">$_\n\n"; print join(", ", @{$names{$_}}), "\n\n"; }
|
|---|