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

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 ); }
  • Comment on Re^11: How to store the output from foreach loop into variable/array without printing?
  • Download Code

Replies are listed 'Best First'.
Re^12: How to store the output from foreach loop into variable/array without printing?
by Anonymous Monk on Mar 14, 2014 at 07:55 UTC
    Bah, I botched the code portion, start with this code please
    #!/usr/bin/perl -- ## by us ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/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_h = [ [ 24, 56, 77, 98, 72], [ 44, 23, 45, 56, 11], [ 23, 11, 25, 67, 91], ]; ## I don't know what you want here ## write it out as if using pencil and paper ## name the vars what you want ## make the program runnable ... like this my $these_first_row_avg = $$d_h[0][0] + $$d_h[0][1] + $$d_h[0][2] + + $$d_h[0][3] + $$d_h[0][4] / 4; my $these_second_row_avg = $$d_h[1][0] + $$d_h[1][1] + $$d_h[1][2] + + $$d_h[1][3] + $$d_h[1][4] / 4; my $these_third_row_avg = $$d_h[2][0] + $$d_h[2][1] + $$d_h[2][2] + + $$d_h[2][3] + $$d_h[2][4] / 4; ## you want these?? my $these_column_d_avg = $$d_h[0][0] + $$d_h[1][0] + $$d_h[2][0] / + 3; my $these_column_e_avg = $$d_h[0][1] + $$d_h[1][1] + $$d_h[2][1] / + 3; my $these_column_f_avg = $$d_h[0][2] + $$d_h[1][2] + $$d_h[2][2] / + 3; my $these_column_g_avg = $$d_h[0][3] + $$d_h[1][3] + $$d_h[2][3] / + 3; my $these_column_h_avg = $$d_h[0][4] + $$d_h[1][4] + $$d_h[2][4] / + 3; ## these are for your final data structure ## what do you want to have here my $these_first = [ $these_first_row_avg ]; my $these_second = [ $these_second_row_avg ]; my $these_third = [ $these_third_row_avg ]; my $final_output_wanted = [ $these_first, $these_second, $these_third, ]; dd( $final_output_wanted ); } ## end sub UsedToBeJustDoWork
      Hi, ok here it is (pardon me..its pretty long):
      #!/usr/bin/perl -- ## by us ## perltidy -olq -csc -csci=10 -cscl="sub : BEGIN END if " -otr -opr +-ce -nibc -i=4 -pt=0 "-nsak=*" #!/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", 446, 0.450, 287.41, 217.76, 231.21, 746.84, 661.86, 812. +52], ["M12", 224, 0.455, 503.91, 579.54, 418.29, 1613.59, 1689.682, 1 +542.565], ["M13", 225, 0.455, 288.69, 358.43, 231.10, 817.81, 996.18, 763. +18], ]; my $d_h = [ [287.41, 217.76, 231.21, 746.84, 661.86, 812.52], [503.91, 579.54, 418.29, 1613.59, 1689.682, 1542.565], [288.69, 358.43, 231.10, 817.81, 996.18, 763.18], ]; ## I don't know what you want here ## ratio calculation # first group my $ratio1_1_1 = $$d_h[0][0] / $$d_h[0][0]; my $ratio1_2_1 = $$d_h[1][0] / $$d_h[0][0]; my $ratio1_3_1 = $$d_h[2][0] / $$d_h[0][0]; my $ratio1_1_2 = $$d_h[0][0] / $$d_h[1][0]; my $ratio1_2_2 = $$d_h[1][0] / $$d_h[1][0]; my $ratio1_3_2 = $$d_h[2][0] / $$d_h[1][0]; my $ratio1_1_3 = $$d_h[0][0] / $$d_h[2][0]; my $ratio1_2_3 = $$d_h[1][0] / $$d_h[2][0]; my $ratio1_3_3 = $$d_h[2][0] / $$d_h[2][0]; my $ratio2_1_1 = $$d_h[0][1] / $$d_h[0][1]; my $ratio2_2_1 = $$d_h[1][1] / $$d_h[0][1]; my $ratio2_3_1 = $$d_h[2][1] / $$d_h[0][1]; my $ratio2_1_2 = $$d_h[0][1] / $$d_h[1][1]; my $ratio2_2_2 = $$d_h[1][1] / $$d_h[1][1]; my $ratio2_3_2 = $$d_h[2][1] / $$d_h[1][1]; my $ratio2_1_3 = $$d_h[0][1] / $$d_h[2][1]; my $ratio2_2_3 = $$d_h[1][1] / $$d_h[2][1]; my $ratio2_3_3 = $$d_h[2][1] / $$d_h[2][1]; my $ratio3_1_1 = $$d_h[0][2] / $$d_h[0][2]; my $ratio3_2_1 = $$d_h[1][2] / $$d_h[0][2]; my $ratio3_3_1 = $$d_h[2][2] / $$d_h[0][2]; my $ratio3_1_2 = $$d_h[0][2] / $$d_h[1][2]; my $ratio3_2_2 = $$d_h[1][2] / $$d_h[1][2]; my $ratio3_3_2 = $$d_h[2][2] / $$d_h[1][2]; my $ratio3_1_3 = $$d_h[0][2] / $$d_h[2][2]; my $ratio3_2_3 = $$d_h[1][2] / $$d_h[2][2]; my $ratio3_3_3 = $$d_h[2][2] / $$d_h[2][2]; #second group my $ratio4_1_1 = $$d_h[0][3] / $$d_h[0][3]; my $ratio4_2_1 = $$d_h[1][3] / $$d_h[0][3]; my $ratio4_3_1 = $$d_h[2][3] / $$d_h[0][3]; my $ratio4_1_2 = $$d_h[0][3] / $$d_h[1][3]; my $ratio4_2_2 = $$d_h[1][3] / $$d_h[1][3]; my $ratio4_3_2 = $$d_h[2][3] / $$d_h[1][3]; my $ratio4_1_3 = $$d_h[0][3] / $$d_h[2][3]; my $ratio4_2_3 = $$d_h[1][3] / $$d_h[2][3]; my $ratio4_3_3 = $$d_h[2][3] / $$d_h[2][3]; my $ratio5_1_1 = $$d_h[0][4] / $$d_h[0][4]; my $ratio5_2_1 = $$d_h[1][4] / $$d_h[0][4]; my $ratio5_3_1 = $$d_h[2][4] / $$d_h[0][4]; my $ratio5_1_2 = $$d_h[0][4] / $$d_h[1][4]; my $ratio5_2_2 = $$d_h[1][4] / $$d_h[1][4]; my $ratio5_3_2 = $$d_h[2][4] / $$d_h[1][4]; my $ratio5_1_3 = $$d_h[0][4] / $$d_h[2][4]; my $ratio5_2_3 = $$d_h[1][4] / $$d_h[2][4]; my $ratio5_3_3 = $$d_h[2][4] / $$d_h[2][4]; my $ratio6_1_1 = $$d_h[0][5] / $$d_h[0][5]; my $ratio6_2_1 = $$d_h[1][5] / $$d_h[0][5]; my $ratio6_3_1 = $$d_h[2][5] / $$d_h[0][5]; my $ratio6_1_2 = $$d_h[0][5] / $$d_h[1][5]; my $ratio6_2_2 = $$d_h[1][5] / $$d_h[1][5]; my $ratio6_3_2 = $$d_h[2][5] / $$d_h[1][5]; my $ratio6_1_3 = $$d_h[0][5] / $$d_h[2][5]; my $ratio6_2_3 = $$d_h[1][5] / $$d_h[2][5]; my $ratio6_3_3 = $$d_h[2][5] / $$d_h[2][5]; ## name the vars what you want ## make the program runnable ... like this my $these_firstgroup_row1_avg = ($ratio1_1_1 + $ratio2_1_1 + $rat +io3_1_1) / 3; my $these_firstgroup_row2_avg = ($ratio1_2_1 + $ratio2_2_1 + $rati +o3_2_1) / 3; my $these_firstgroup_row3_avg = ($ratio1_3_1 + $ratio2_3_1 + $rat +io3_3_1)/ 3; my $these_firstgroup_row4_avg = ($ratio1_1_2 + $ratio2_1_2 + $rat +io3_1_2) / 3; my $these_firstgroup_row5_avg = ($ratio1_2_2 + $ratio2_2_2 + $rati +o1_2_2) / 3; my $these_firstgroup_row6_avg = ($ratio1_3_2 + $ratio2_3_2 + $rat +io3_3_2)/ 3; my $these_firstgroup_row7_avg = ($ratio1_1_3 + $ratio2_1_3 + $rat +io3_1_3) / 3; my $these_firstgroup_row8_avg = ($ratio1_2_3 + $ratio2_2_3 + $rati +o3_2_3) / 3; my $these_firstgroup_row9_avg = ($ratio1_3_3 + $ratio2_3_3 + $rat +io3_3_3)/ 3; my $these_secondgroup_row1_avg = ($ratio4_1_1 + $ratio5_1_1 + $ra +tio6_1_1) / 3; my $these_secondgroup_row2_avg = ($ratio4_2_1 + $ratio5_2_1 + $rat +io6_2_1) / 3; my $these_secondgroup_row3_avg = ($ratio4_3_1 + $ratio5_3_1 + $ra +tio6_3_1)/ 3; my $these_secondgroup_row4_avg = ($ratio4_1_2 + $ratio5_1_2 + $ra +tio6_1_2) / 3; my $these_secondgroup_row5_avg = ($ratio4_2_2 + $ratio5_2_2 + $rat +io6_2_2) / 3; my $these_secondgroup_row6_avg = ($ratio4_3_2 + $ratio5_3_2 + $ra +tio6_3_2)/ 3; my $these_secondgroup_row7_avg = ($ratio4_1_3 + $ratio5_1_3 + $ra +tio6_1_3) / 3; my $these_secondgroup_row8_avg = ($ratio4_2_3 + $ratio5_2_3 + $rat +io6_2_3) / 3; my $these_secondgroup_row9_avg = ($ratio4_3_3 + $ratio5_3_3 + $ra +tio6_3_3)/ 3; ## you want these?? not needed # my $these_column_d_avg = $$d_h[0][0] + $$d_h[1][0] + $$d_h[2][0] +/ 3; # my $these_column_e_avg = $$d_h[0][1] + $$d_h[1][1] + $$d_h[2][1] +/ 3; # my $these_column_f_avg = $$d_h[0][2] + $$d_h[1][2] + $$d_h[2][2] +/ 3; # my $these_column_g_avg = $$d_h[0][3] + $$d_h[1][3] + $$d_h[2][3] +/ 3; # my $these_column_h_avg = $$d_h[0][4] + $$d_h[1][4] + $$d_h[2][4] +/ 3; ## these are for your final data structure ## what do you want to have here my $these_firstgroup_row1 = [ $these_firstgroup_row1_avg ]; my $these_firstgroup_row2 = [ $these_firstgroup_row2_avg ]; my $these_firstgroup_row3 = [ $these_firstgroup_row3_avg ]; my $these_firstgroup_row4 = [ $these_firstgroup_row4_avg ]; my $these_firstgroup_row5 = [ $these_firstgroup_row5_avg ]; my $these_firstgroup_row6 = [ $these_firstgroup_row6_avg ]; my $these_firstgroup_row7 = [ $these_firstgroup_row7_avg ]; my $these_firstgroup_row8 = [ $these_firstgroup_row8_avg ]; my $these_firstgroup_row9 = [ $these_firstgroup_row9_avg ]; my $these_secondgroup_row1 = [ $these_secondgroup_row1_avg ]; my $these_secondgroup_row2 = [ $these_secondgroup_row2_avg ]; my $these_secondgroup_row3 = [ $these_secondgroup_row3_avg ]; my $these_secondgroup_row4 = [ $these_secondgroup_row4_avg ]; my $these_secondgroup_row5 = [ $these_secondgroup_row5_avg ]; my $these_secondgroup_row6 = [ $these_secondgroup_row6_avg ]; my $these_secondgroup_row7 = [ $these_secondgroup_row7_avg ]; my $these_secondgroup_row8 = [ $these_secondgroup_row8_avg ]; my $these_secondgroup_row9 = [ $these_secondgroup_row9_avg ]; my $final_output_wanted = [ $these_firstgroup_row1, $these_secondgroup_row1, $these_firstgroup_row2, $these_secondgroup_row2, $these_firstgroup_row3, $these_secondgroup_row3, $these_firstgroup_row4, $these_secondgroup_row4, $these_firstgroup_row5, $these_secondgroup_row5, $these_firstgroup_row6, $these_secondgroup_row6, $these_firstgroup_row7, $these_secondgroup_row7, $these_firstgroup_row8, $these_secondgroup_row8, $these_firstgroup_row9, $these_secondgroup_row9, ]; dd ( $final_output_wanted ); } ## end sub UsedToBeJustDoWork _Output Data_ [ [1], [1], ["2.07"], ["2.20"], ["1.21"], ["1.17"], ["0.49"], ["0.46"], [1], [1], ["0.58"], ["0.53"], ["0.86"], ["0.88"], ["1.72"], ["1.89"], [1], [1], ]
      Am able to run the code and get the output correctly. So what's the next step I should take?

        :) I think we should hae done fewer columns ...

        So next step would be, instead of doing  my $ratio1_1_1... (pencil and paper), write a function to create that array (like a real array not named scalars)

        Which I did , I replicated your program using functions

         

        Does this help you or would you like to see more?

        Anything "click" for you after seeing makeRatio ? What clicked

        Would you like to try writing the next function that makes use of \@ratio?

        Or would you like to see the rest of my program (makeRatioAvg and makeFinal) ?