in reply to Combining hashes of hahses?

I like Grandfather's solution. Mine reads the records into a temporary structure, only to then later get the unique species and associate the dna and morph strings. Mine also had hardcoded the length of the output part (dna or morph), but I did that since I wasn't sure if they were a fixed length or if they could be variable. Easily handled as Grandfather does in his program.
# Read in DNA file (file.1) my %dna = (); open my $file1, "<", "./file.1" or die "Can't open file.1: $!"; while (<$file1>) { chomp; next if $. == 1; my ($key, $val) = split /\s+/; $dna{$key} = $val; } # Read in MORPH file (file.2) my %morph = (); open my $file2, "<", "./file.2" or die "Can't open file.2: $!"; while (<$file2>) { chomp; next if $. == 1; my ($key, $val) = split /\s+/; $morph{$key} = $val; } # Get sorted, unique keys from above my %allkeys = map { $_ => 1 } (sort keys %dna, sort keys %morph); my @uniq_species = sort keys %allkeys; my %records = (); foreach (@uniq_species) { $records{$_} = (defined $dna{$_}) ? $dna{$_} : "?"x14; $records{$_} .= (defined $morph{$_}) ? $morph{$_} : "?"x18; } foreach (sort keys %records) { print "$_ $records{$_}\n"; }

---
echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Replies are listed 'Best First'.
Re^2: Combining hashes of hahses?
by erio (Initiate) on Nov 07, 2007 at 18:41 UTC
    Thanks tuxz0r. Learned much from both your and GrandFather's scripts. The length of both data partitions can be variable. Cheers!