CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
How to store the output from foreach loop into variable/array without printing out?

 



hellohello1
Novice

Mar 10, 2014, 11:41 PM

Post #1 of 5 (1340 views)
How to store the output from foreach loop into variable/array without printing out? Can't Post

Hello!

I have a question. I have a code:
(OUT1 refers to textfile)


Code
 
sub data
{
for $arr_ref1 (@full_data)
{
for $arr_ref2(@full_data)
{
for $index (3..$#$arr_ref2)
{

$ratio = sprintf( "%.4f%s", $$arr_ref2[$index]/$$arr_ref1[$index],"\t");
print OUT1 "$ratio";

}
print OUT1 "\n"; #print D to E columns into next line after one loop
}

}
}

_DATA_ (A to G letters is to indicate column location)
A B C D E F G
AX8 0.27 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) :

Code
 for $index (3..$#$arr_ref2) 
{

$ratio = sprintf( "%.4f%s", $$arr_ref2[$index]/$$arr_ref1[$index],"\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 I 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:

Code
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?

(This post was edited by hellohello1 on Mar 10, 2014, 11:42 PM)


Laurent_R
Veteran / Moderator

Mar 11, 2014, 12:23 AM

Post #2 of 5 (1337 views)
Re: [hellohello1] How to store the output from foreach loop into variable/array without printing out? [In reply to] Can't Post

Cross-posted on Perl Monks.


hellohello1
Novice

Mar 11, 2014, 12:58 AM

Post #3 of 5 (1334 views)
Re: [Laurent_R] How to store the output from foreach loop into variable/array without printing out? [In reply to] Can't Post

Hi Laurent, is it okay or against the rules to cross post?


Laurent_R
Veteran / Moderator

Mar 11, 2014, 10:46 AM

Post #4 of 5 (1326 views)
Re: [hellohello1] How to store the output from foreach loop into variable/array without printing out? [In reply to] Can't Post

Hi, no problem with cross-posting, but it is usually considered appropriate to warn about it upfront (and to provide links) so as to avoid duplicate work on two different sites.


hellohello1
Novice

Mar 11, 2014, 6:15 PM

Post #5 of 5 (1322 views)
Re: [Laurent_R] How to store the output from foreach loop into variable/array without printing out? [In reply to] Can't Post

Ok! Thanks! :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives