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

I know its got to do with the placement of the $variable1 .= "$ratio1"; ...but I have simply no idea how to go on and correct from there.

Sounds like you're not Coping with Scoping (tutorial and diagnosis)

If you can post more than a a fragment, something I can run (like your current short self contained program), I can explain what is going on

  • Comment on Re^5: How to store the output from foreach loop into variable/array without printing?

Replies are listed 'Best First'.
Re^6: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 14, 2014 at 02:03 UTC
    Hi, ok. Here's my actual code:
    while(<CURINFILE>) { push @full_data , [split] while (<CURINFILE>); for $arr_ref1 (@full_data) { for $arr_ref2(@full_data) { print OUT1 "$$arr_ref1[0]\t$$arr_ref1[1]\t$$arr_ref1[2]\t\t"; #prin +t first three columns of input data into textfile $firstend = $firstinput + 2; $start2 = $firstend + 1; $secondend = $firstend + $secondinput; for my $index1 (3..$firstend)#column 4 to end of group 1 column { $ratio1 = sprintf( "%.4f%s", $$arr_ref2[$index1]/$$arr_ref1[$inde +x1],"\t"); print OUT1 "$ratio1"; #$variable1 .= "$ratio1"; #attempting to store values in $variabl +e1 } for my $index2 ($start2..$secondend)#start of group 2 column to +end) { $ratio2 = sprintf( "%.4f%s", $$arr_ref2[$index2]/$$arr_ref1[$ind +ex2],"\t"); print OUT1 "$ratio2"; #$variable2 .= "$ratio2"; #attempting to store values in $variab +le1 } print OUT1 "\n"; #print next line after one loop } # finish one loop of one ID }
    $arr_ref1(@full_data)and $arr_ref2(@full_data) refer to the same input of data from D to the end of data. Input DATA (Column letter is just to indicate my data)
    A: ID B, C (time) C to E: Group 1 F to H: Group 2 # Calculation is taking the ratio of each row per column _Input DATA_ A B C D E F G H M11 0.1 10 24 56 77 98 72 M12 0.3 13 44 23 45 56 11 M13 0.4 54 23 11 25 67 91

    What my code on top does is to output this:

    _OUTPUT DATA_ A B C D E F G H I M11 0.1 10 1 1 1 1 1 M12 0.3 13 1.83 0.41 0.58 0.57 0.15 M13 0.4 54 0.95 0.19 0.32 0.68 1.26 . . .
    Right now, I do not want to print the output out into textfile because I have huge dataset. So I am attempting to take output of the column E to I and put into variable and then be used to calculate the average by each group and row. That is why I put two groups (for loop) separately. After which, the data will be filtered and print out the desired output.

    Hence, I add the $variable1 and $variable2 to join the values of each cell together. But the output become like this:

    _OUTPUT DATA_ A B C D E F G H I J K M11 0.1 10 1 1 1 M11 0.1 10 1 1 1 1.83 0.95 M11 0.1 10 1 1 1 1.83 0.95 1 0.41 M11 0.1 10 1 1 1 1.83 0.95 1 0.41 . . .
    I am really stuck here. Thanks for your patience in guiding me so far!

      Well, thats not what I had in mind :)

      Can you describe/explain what this loop does in one sentence? Your explanation will help me write a response you can understand (hopefully)

      for $arr_ref1 ( @full_data ) { for $arr_ref2 ( @full_data ) { } }
        Oops. Ok. I store the data (column D onwards) into the array because I need to calculate the ratio between each rows. So I store into two arrays such that I can take 1st array/2nd array.
        _Input DATA_ A B C D E F G H M11 0.1 10 24 56 77 98 72 M12 0.3 13 44 23 45 56 11 M13 0.4 54 23 11 25 67 91
        Store values from D to H into $arr_ref1 ( @full_data ) and $arr_ref2 ( @full_data ) and divide from there:
        _INPUT DATA_ D E F G H 24 56 77 98 72 44 23 45 56 11 23 11 25 67 91 _OUTPUT DATA_ 1 1 1 1 1 1.83 0.41 0.58 0.57 0.15 0.95 0.19 0.32 0.68 1.26
        I hope my explanation is ok. :)