http://www.perlmonks.org?node_id=1077797

hellohello1 has asked for the wisdom of the Perl Monks concerning the following question:

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?

Replies are listed 'Best First'.
Re: How to store the output from foreach loop into variable/array without printing?
by kcott (Archbishop) on Mar 11, 2014 at 07:47 UTC
      Hi Kcott, yes I did add the strict pragmata, which prompt me that error. Have been trying to figure out how to work out my code by trying array method, etc.

      Pardon my limited knowledge, if I want to put push statement into the for loop, the $ratio1 will be added into the @data1 one by one until one loop is finished?

      I did try putting in the for loop, but I still couldn't get the output. So I try trial and error and end up writing to you guys for help and wonder if there is a better way to do it.

      Thanks :) I am looking into the earlier advice :)

Re: How to store the output from foreach loop into variable/array without printing?
by vinoth.ree (Monsignor) on Mar 11, 2014 at 07:31 UTC

    Hi,

    As per my understanding from your post, that you need data from __DATA__ area 3..4 and 5..6 in separate array of each row. I have saved data as another array ref, you can do deref and you can take data from each array.

    There could be more efficient way, other than this.

    use strict; use warnings; use Data::Dumper; my @arr = <DATA>; chomp @arr; my (@Arry1,@Arry2); foreach(@arr) { my @arr1 = split(/\s+/,$_); push (@Arry1,[@arr1[3..4]]); push (@Arry2,[@arr1[5..6]]); } print Dumper \@Arry1; print Dumper \@Arry2; __DATA__ 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

    All is well
      Thanks! I'll think through and work it out :)
Re: How to store the output from foreach loop into variable/array without printing?
by Anonymous Monk on Mar 11, 2014 at 06:54 UTC

    Or is there other way to do it?

    There is a better way,

    Are you interested?

      Hi, Yes! I am interested. :)