I turned your data into a tab-separated CSV file and added a line of headers (fieldnames). Then I could use DBI to attack your problem with SQL.
use Modern::Perl; use DBI; my $dbh = DBI->connect( "dbi:CSV:", "", "", { f_dir => './DB', csv_sep_char => "\t", } ) or die "Cannot connect: $DBI::errstr"; my $sql = 'SELECT id, count(*) AS number, sum(tumor) AS tumorscore, sum(normal) +AS normalscore, sum(tumor - normal) AS difference, avg(tumor - normal +) AS averagedifference FROM methyl WHERE (tumor - normal) <=20 or (tu +mor - normal) >=20 GROUP BY id ORDER BY id'; my $sth = $dbh->prepare($sql); $sth->execute; while ( my $row = $sth->fetchrow_hashref ) { say "id: $row->{'id'}, Count: $row->{'number'}, Score tumor: $row->{'tumor +score'}, Score normal: $row->{'normalscore'}, Difference: $row->{'dif +ference'}, Average Difference: $row->{'averagedifference'}"; }
Results:
id: cg00381604, Count: 3, Score tumor: 99, Score normal: 174, Differen +ce: -75, Average Difference: -25 id: cg03130891, Count: 1, Score tumor: 55, Score normal: 84, Differenc +e: -29, Average Difference: -29 id: cg12045430, Count: 3, Score tumor: 129, Score normal: 174, Differe +nce: -45, Average Difference: -15 id: cg13869341, Count: 1, Score tumor: 908, Score normal: 913, Differe +nce: -5, Average Difference: -5 id: cg14008030, Count: 1, Score tumor: 688, Score normal: 776, Differe +nce: -88, Average Difference: -88 id: cg20253340, Count: 1, Score tumor: 560, Score normal: 593, Differe +nce: -33, Average Difference: -33 id: cg20826792, Count: 3, Score tumor: 171, Score normal: 174, Differe +nce: -3, Average Difference: -1 id: cg21870274, Count: 1, Score tumor: 791, Score normal: 809, Differe +nce: -18, Average Difference: -18
To exclude as much as possible any rounding-of errors I did not divide the figures by 1000.

CountZero

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: compare values while value on other column remains the same by CountZero
in thread compare values while value on other column remains the same by linseyr

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.