in reply to Re^7: 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?

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. :)

Replies are listed 'Best First'.
Re^9: How to store the output from foreach loop into variable/array without printing?
by Anonymous Monk on Mar 14, 2014 at 06:45 UTC

    I hope my explanation is ok. :)

    :) I wanted to hear something more basic, what you described is like a statement of goals

    Consider this loop

    for $one ( 1 .. 10 ){ for $two ( 1 .. 10 ){ } }

    Please can you describe what it does?

      Using nested for loop to compare all elements in the array to each other?

        Ok, thank you,

        Ok, second take, lets try like this , slightly less socratic, trying not to figure out where you're stuck, but pretending as if this is your first step in solving this problem, and you're using computer instead of pencil and paper

        Please fill in the blanks in program below , edit sub UsedToBeJustDoWork

        Create however many variables you need, change whatever names you want, in the end the program should run and produce the data structure you want your program to have

        you should not use foreach or while, just type out the vars and math you need to get the correct by your definition of $final_output_wanted

        #!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { UsedToBeJustDoWork(); } sub UsedToBeJustDoWork { my $full_data = [ ["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], ]; my $d_to_h = [ [ 24, 56, 77, 98, 72], [ 44, 23, 45, 56, 11], [ 23, 11, 25, 67, 91], ]; ## I don't know what you want here my $these_first_row_average = $d_to_h->[0] + $d_to_h->[1] + $d_to_ +h->[2] + $d_to_h->[3] + $d_to_h->[4] / 4; my $final_output_wanted = [ ## I don't know what you want here $these_first, $these_second, $these_third, ]; dd( $final_output_wanted ); }