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 code. >B Bob, Barbara >C Cane, Carter EOF my @files = (\$file1, \$file2, \$file3); # References to the scalars above. my %names = (); # Oh, you know... a hash. for my $f (@files) { # Iterate through the references 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 newlines. 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 line? my @names = split(/, /, $_); # Then it's a list of names! $tmp_names{$key} = \@names; # Store them in the temporary 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"; }