Hi Monks! Here's what my data looks like:
File#1 dna: species_1 ACCATGATACGATG species_2 GGTTTCGACGCAGA species_3 GGACTCAGCGACTA File#2 morph: species_1 001001010201001001 species_2 002010200210120201 species_4 001001110000000101 species_5 111001001001000201
If both data types exist for a species, I would like to append the morph data to the dna data. If a species is missing one of the data types, I would like to append as many "?"s as there are elements in the missing data partition:
species_1 ACCATGATACGATG001001010201001001 species_2 GGTTTCGACGCAGA002010200210120201 species_3 GGACTCAGCGACTA?????????????????? species_4 ??????????????001001110000000101 species_5 ??????????????111001001001000201
I am completely new to Perl. What is the best strategy here? I tried constructing an hash of anonymous array refs:
open IN, "dnamorph.txt" or die $!; my %data; my $taxon; while (<IN>) { if (/(^\w+)(\t+)(\w+)/) { $taxon = $1; push @{ $data{$taxon} }, $3; } }
Here, I couldn't figure out how to recognize if one of the partitions is missing and fill it with "?"s. My hash of de-refed arrays looked like this:
species_1 ACCATGATACGATG001001010201001001 species_2 GGTTTCGACGCAGA002010200210120201 species_3 GGACTCAGCGACTA species_4 001001110000000101 species_5 111001001001000201
I also tried to make a hash of hashes for each data partition, eg:
open DNAA, "dna.nxs" or die $!; my %ddata; while (<DNAA>) { if (/(^\w+)(\t+)(\w+)/) { $ddata{ $1} = { dna => $3, }; } }
My data looks likes this:
species_six: dna=TTGGGACAGCCGAGGCACGA species_two: dna=AAAATCGGGCGGCGCTTTTC species_five: dna=TTCCAGGACATCGGCATACG species_three: dna=GGGGCCCCAATATCGATACG species_four: dna=GGGGAGGACGTAGATATTAT species_one: dna=ACTGTTTCGTAGGGCTAGGA species_two: morph=111101011011011 species_five: morph=111101011011011 species_three: morph=012111011011011 species_four: morph=112111011011011 species_one: morph=110111011011111
Here, I can't figure out how to combine the 2 hashes of hashrefs without clobbering some values. Any help would be much apreciated, erio

In reply to Combining hashes of hahses? by erio

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.