Hi,

ok. I actually continue using my code as I feel more familiarize with it but did accordingly to how you advise me and I can come out with the output I want:

while(<CURINFILE>) { push @full_data , [split] while (<CURINFILE>); for $arr_ref1 (@full_data) { for $arr_ref2(@full_data) { $variable1 = ''; $variable2 = ''; for my $index1 (3..4) { $ratio1 = sprintf( "%.4f%s", $$arr_ref2[$index1]/$$arr_ref1 +[$index1],"\t"); $variable1 .= $ratio1; } for my $index2 (5..6) { $ratio2 = sprintf( "%.4f%s", $$arr_ref2[$index2]/$$arr_ref1 +[$index2],"\t"); $variable2 .= $ratio2; } #Calculate Average @arrayint1 = split (/\t/,$variable1); $avg1 = &average (\@arrayint1); #print OUT1 "\t$avg1"; @arrayint2 = split (/\t/,$variable2); $avg2 = &average (\@arrayint2); #print OUT1 "\t$avg2"; #Calculate SD @arrayint1 = split (/\t/,$variable1); $std1 = &stdev(\@arrayint1); #print OUT1 "\t$std1"; @arrayint2 = split (/\t/,$variable2); $std2 = &stdev(\@arrayint2); #print OUT1 "\t$std2"; #Calculate CV $cv1 = $std1/$avg1; $cv2 = $std2/$avg2; my $outputa = "$$arr_ref1[0]"; my $outputb = "$variable1\t$variable2\t"; my $outputc = "\t$avg1\t$avg2"; my $outputd = "\t$cv1\t$cv2"; ###### print everything out ##################### my $key = "$outputa"."$outputb". "$outputc"."outputd"; print OUT1 $key; print OUT1 "\n"; } } sub average{ my($data) = @_; if (not @$data) { die("Empty array\n"); } my $total = 0; foreach (@$data) { $total += $_; } my $average = $total / @$data; return $average; } sub stdev{ my($data) = @_; if(@$data == 1){ return 0; } my $average = &average($data); my $sqtotal = 0; foreach(@$data) { $sqtotal += ($average-$_) ** 2; } my $std = ($sqtotal / (@$data-1)) ** 0.5; return $std; }
so the output will be :
_OUTPUT_ M446T27 1 1 1 1 1 1 0 0 M446T27 1.75 2.66 1.80 2.16 2.20 1.98 0.29 0.12 M446T27 1.00 1.64 0.99 1.09 1.32 1.04 0.34 0.06 . .
So far so good. Phew! Thanks to your guidance! :)

My next question is, is it possible to actually filter the whole row based on the outputd(set condition)?

(not sure if it should be put into a new thread?)

In reply to Re^22: How to store the output from foreach loop into variable/array without printing? by hellohello1
in thread How to store the output from foreach loop into variable/array without printing? by hellohello1

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.