in reply to Combining hashes of hahses?
An issue is that push @{ $data{$taxon} }, $3; does not make sense - you intimate that data is a hash of hash, but you are using it as a hash of array in that statement.
Without more information telling us what you want to do with the data it's not clear if the data structures you are generating are appropriate. Consider the following sample however:
use strict; use warnings; my $file1 = <<FILE; dna: species_1 ACCATGATACGATG species_2 GGTTTCGACGCAGA species_3 GGACTCAGCGACTA FILE my $file2 = <<FILE; morph: species_1 001001010201001001 species_2 002010200210120201 species_4 001001110000000101 species_5 111001001001000201 FILE my %data; my $dnaLen = 0; my $morphLen = 0; open IN, '<', \$file1 or die "Failed to open file1: $!"; while (<IN>) { next unless /(^\w+)\s+(\w+)/; $data{$1}{dna} = $2; $dnaLen ||= length $2; } close IN; open IN, '<', \$file2 or die "Failed to open file2: $!"; while (<IN>) { next unless /(^\w+)\s+(\w+)/; $data{$1}{morph} = $2; $morphLen ||= length $2; } close IN; die "No dna data found" unless $dnaLen; die "No morph data found" unless $morphLen; for my $species (sort keys %data) { $data{$species}{dna} ||= '?' x $dnaLen; $data{$species}{morph} ||= '?' x $morphLen; print "$species: $data{$species}{dna}$data{$species}{morph}\n"; }
Prints:
species_1: ACCATGATACGATG001001010201001001 species_2: GGTTTCGACGCAGA002010200210120201 species_3: GGACTCAGCGACTA?????????????????? species_4: ??????????????001001110000000101 species_5: ??????????????111001001001000201
which uses a hash of hash where the primary key is the species and the secondary key is morph or dna.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Combining hashes of hahses?
by erio (Initiate) on Nov 07, 2007 at 18:38 UTC | |
by GrandFather (Saint) on Nov 07, 2007 at 19:53 UTC | |
by erio (Initiate) on Nov 08, 2007 at 01:03 UTC | |
|
Re^2: Combining hashes of hahses?
by convenientstore (Pilgrim) on Nov 07, 2007 at 21:48 UTC | |
by GrandFather (Saint) on Nov 07, 2007 at 22:09 UTC | |
by convenientstore (Pilgrim) on Nov 07, 2007 at 22:20 UTC |