Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
One split can get you your data:
use Modern::Perl; while (<DATA>) { chomp; my ($id, $sample_id, undef, $animal, undef, $color) = split /[\t_; +]+/; say "$id ($sample_id) is a $animal colored $color"; } __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
occurence1 (A) is a bear colored black occurence2 (B) is a wolf colored grey occurence3 (A) is a wolf colored white occurence4 (A) is a bear colored occurence5 (C) is a wolf colored grey occurence6 (C) is a bear colored brown
Now that we have the data, why not putting it into a database such as SQLite?
use Modern::Perl; use DBI; my $dbh = DBI->connect( "dbi:SQLite:dbname=c:/data/strawberry-perl/perl/script-chrome/animals. +sqlite", "", "" ) or die "Could not open database"; my $statement = q{CREATE TABLE IF NOT EXISTS "main"."sightings" ("id" TEXT PRIMARY KEY + NOT NULL, "sampleid" TEXT, "animal" TEXT, "color" TEXT)}; my $rows = $dbh->do($statement) or die $dbh->errstr; $dbh->{AutoCommit} = 0; $dbh->do('BEGIN TRANSACTION'); my $sth = $dbh->prepare( q{INSERT INTO sightings (id, sampleid, animal, color) VALUES (?, ? +, ?, ?)}) or die "Could not prepare INSERT statement"; while (<DATA>) { chomp; my ( $id, $sample_id, undef, $animal, undef, $color ) = split /[\t +_;]+/; $color ||= 'unknown'; $sth->execute( $id, $sample_id, $animal, $color ); } $dbh->commit;
Now we can interrogate the database and use aggregate functions to get your answers:
SELECT sampleid, animal, count(*) FROM sightings GROUP BY sampleid, an +imal
will return
"A","bear","6" "A","wolf","4" "B","bear","2" "B","wolf","7" "C","bear","6" "C","wolf","5"
SELECT sampleid, color, count(*) FROM sightings GROUP BY sampleid, col +or
will give you
"A","black","1" "A","brown","3" "A","grey","1" "A","red","1" "A","unknown","3" "A","white","1" "B","brown","1" "B","grey","5" "B","red","1" "B","unknown","2" "C","black","1" "C","brown","4" "C","grey","3" "C","unknown","2" "C","white","1"


A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

My blog: Imperial Deltronics

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

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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others exploiting the Monastery: (3)
    As of 2018-02-21 21:56 GMT
    Find Nodes?
      Voting Booth?
      When it is dark outside I am happiest to see ...

      Results (288 votes). Check out past polls.