Hello! I have a question. I have a code: (OUT1 refers to textfile)
sub data { for $arr_ref1 (@full_data) { for $arr_ref2(@full_data) { for $index (3..$#$arr_ref2) { $ratio = sprintf( "%.2f%s", $$arr_ref2[$index]/$$arr_ref1[$ind +ex],"\t"); print OUT1 "$ratio"; } print OUT1 "\n"; #print D to E columns into next line +after one loop } } } _DATA_ (A to G is just to indicate column location) A B C D E F G AX8 0.23 42.4 1 1 1 1 AX4 0.65 53.6 0.2 7.8 9.1 2.3 AX12 0.34 23.9 3.2 7.2 1.2 9.3
My problem here is the (print OUT1 "$ratio";) line onwards. Instead of printing the output into textfile, is it possible to print the output into an array or something so that I can carry on with the calculation of the output from there? I have tried something like this (push data into array) :
for $index (3..$#$arr_ref2) { $ratio = sprintf( "%.2f%s", $$arr_ref2[$index]/$$arr_ref1[$ind +ex],"\t"); #print OUT1 "$ratio"; } print OUT1 "\n"; #print D to E columns into next line +after one loop push (@data1, $ratio1); print OUT1 ($data1); }
But am not getting the desired output. I just can't figure out how to get those output and store into variables/arrays instead of printing it out. My desired outcome is to put:
D and E (by rows) --> 1st array F and G (by rows) --> 2nd array
so that I can calculate their average and standard deviation for both arrays and put into the next column beside G. Or is there other way to do it?

In reply to 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.