in reply to Re: Combining hashes of hahses?
in thread Combining hashes of hahses?

Thanks GrandFather. Very helpful. Sorry to have been a bit light on context. I am trying to put DNA sequence data and coded morphological data into a file format that is accepted by a number of programs that reconstruct the evolutionary relationships amongst a group of organisms. The file will look something like this:
#nexus begin data; dimensions ntax=5 nchar=32; format datatype=mixed (dna:1-14, standard:15-32) missing=? gap=-; Matrix species_1: ACCATGATACGATG001001010201001001 species_2: GGTTTCGACGCAGA002010200210120201 species_3: GGACTCAGCGACTA?????????????????? species_4: ??????????????001001110000000101 species_5: ??????????????111001001001000201 end;
The number of species and characters can be quite large.

Replies are listed 'Best First'.
Re^3: Combining hashes of hahses?
by GrandFather (Saint) on Nov 07, 2007 at 19:53 UTC

    In that case the hash of hash is exactly appropriate and it looks like my sample code should drop right into your application. Happy to help.

    Update: there is enough information to generate the header too: ;)

    ... my @species = sort keys %data; my $nSpecies = @species; # Print the header print "begin data;\n"; printf "dimensions ntax=%d nchar=%d;\n", $nSpecies, $dnaLen + $morphLe +n; printf "format datatype=mixed (dna:1-%d, standard:%d-%d) missing=? ga +p=-;\n", $dnaLen, $dnaLen + 1, $dnaLen + $morphLen; print "Matrix\n"; # Print the data 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"; } print "end;\n";

    Prints:

    begin data; dimensions ntax=5 nchar=32; format datatype=mixed (dna:1-14, standard:15-32) missing=? gap=-; Matrix species_1: ACCATGATACGATG001001010201001001 species_2: GGTTTCGACGCAGA002010200210120201 species_3: GGACTCAGCGACTA?????????????????? species_4: ??????????????001001110000000101 species_5: ??????????????111001001001000201 end;

    Perl is environmentally friendly - it saves trees
      You are wise GrandFather, I had tried something similar without the benefit of a comprehension of printf, which makes things a bit tidier. Cheers!