Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Hi ejohnston7,

Hash to the rescue... Using the data given by the OP. One can do like so:

use warnings; use strict; my %analyse; while (<DATA>) { chomp; if ( my ( $animal_type, $animal_name, $animal_color ) = m/.+?([A-Z]).+?__(.+?);.+?__(.+?)?$/ ) { $analyse{$animal_name}{$animal_type}++; $analyse{$animal_color}{$animal_name}{$animal_type}++ if defined $animal_color; } } use Data::Dump; dd \%analyse; __DATA__ occurence1 A a__bear;c__black occurence2 B a__wolf;c__grey occurence3 A a__wolf;c__white occurence4 A a__bear;c__ occurence5 C a__wolf;c__grey occurence6 C a__bear;c__brown occurence7 A a__wolf;c__ occurence8 B a__wolf;c__ occurence9 C a__bear;c__black occurence10 C a__wolf;c__ occurence11 A a__wolf;c__red occurence12 B a__wolf;c__grey occurence13 C a__wolf;c__grey occurence14 C a__wolf;c__grey occurence15 B a__bear;c__brown occurence16 C a__bear;c__brown occurence17 A a__bear;c__ occurence18 A a__bear;c__brown occurence19 C a__wolf;c__white occurence20 B a__wolf;c__grey occurence21 B a__bear;c__ occurence22 B a__wolf;c__grey occurence23 A a__wolf;c__grey occurence24 A a__bear;c__brown occurence25 C a__bear;c__brown occurence26 A a__bear;c__brown occurence27 C a__bear;c__ occurence28 C a__bear;c__brown occurence29 B a__wolf;c__red occurence30 B a__wolf;c__grey
The output shows the two tables together. The desired output is left for the OP to make happen.
Output from the code above:
{ bear => { A => 6, B => 2, C => 6 }, black => { bear => { A => 1, C => 1 } }, brown => { bear => { A => 3, B => 1, C => 4 } }, grey => { wolf => { A => 1, B => 5, C => 3 } }, red => { wolf => { A => 1, B => 1 } }, white => { wolf => { A => 1, C => 1 } }, wolf => { A => 4, B => 7, C => 5 }, }
Update:
And if you just care about just getting the color, with the frequency of type, then you can change this:
$analyse{$animal_color}{$animal_name}{$animal_type}++ if defined $animal_color;
to this
$analyse{$animal_color}{$animal_type}++ if defined $animal_color;
then your output would be:
{ bear => { A => 6, B => 2, C => 6 }, black => { A => 1, C => 1 }, brown => { A => 3, B => 1, C => 4 }, grey => { A => 1, B => 5, C => 3 }, red => { A => 1, B => 1 }, white => { A => 1, C => 1 }, wolf => { A => 4, B => 7, C => 5 }, }

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me

In reply to Re: Consolidating biological data into occurance of each biological unit per sample in table by 2teez
in thread Consolidating biological data into occurance of each biological unit per sample in table by ejohnston7

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!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others avoiding work at the Monastery: (6)
    As of 2014-12-25 14:04 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      Is guessing a good strategy for surviving in the IT business?





      Results (160 votes), past polls