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

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

Replies are listed 'Best First'.
Re^13: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 14, 2014 at 10:05 UTC
    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) ?

        Hi! Ok. Been trying out writing the function similar to yours for past two days and I have a question here.

        I tried running the similar code on my script and I get the error message whereby it stated that

        can't use string ("0") as an ARRAY ref while "strict refs" in use at l +ine 119. Only hard reference allowed by "strict refs".
        SO I change the value here for this part (0 to 3) and I add in the main for loop:
        for my $dataratio(@full_data){ for my $six ( 3 .. $columns ){ for my $tre ( 3 .. $rows ){ for my $tri ( 3 .. $rows ){ } } }
        can't use string ("M11") as an ARRAY ref while "strict refs" in use at + line 119. Only hard reference allowed by "strict refs".
        Ok. i think I am doing something wrong here. So for my( $d_h ) = @_; how do you actually exclude out the first three columns of full_data (e.g. M11, M12..)? Another question is this code:
        1+$six, 1+$tri, 1+$tre, $tri, $six, $tre, $six, ;;; $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] + / $$d_h[ $tre ][ $six ] ;
        I do not understand how this part works. Hope you can nudge me in the right direction?