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

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

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

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

        I do not understand how this part works. Hope you can nudge me in the right direction?

        If you'd be willing to be more specific I'd be willing to elaborate -- maybe you can explain which parts/characters you do understand and which parts you're hazy on

        Hi, ok. Let me clarify whether if it means what I thought it is for some parts of the code.
        sub makeRatio { my( $d_h ) = @_; my $rows = $#$d_h ; ## last index of array my $columns = $#{ $d_h->[0] }; ## last index of first row my @ratio;
        $d_h refers to the dataset from column 4 onwards excluding the first three columns. my $columns : storing the last index of $_[0]
        for my $six ( 0 .. $columns ){ for my $tre ( 0 .. $rows ){ for my $tri ( 0 .. $rows ){ ## for visual matching printf 'my $ratio%d_%d_%d = $$d_h[%d][%d] / $$d_h[%d] +[%d];'."\n",
        I am looping through 6 columns ($six), and in each column, I will do ratio against two values ($tre and $tri).

        $ratio%d_%d_%d refers to the ratio between two values in decimal. (But why three %d?)

        1+$six, 1+$tri, 1+$tre, $tri, $six, $tre, $six, ;;; $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] + / $$d_h[ $tre ][ $six ] ; } } } return \@ratio ; }

        1+$six, 1+$tri, 1+$tre, refer to looping next loop?

        what does this addition of code do?
        $tri, $six, $tre, $six,

        Pardon my limited knowledge but it is the first time I see the statement ended with "," instead of ";" , instead the ";" is added below the functions. So may I know if there is any article that explain that or..?

        And is
        $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] / $$d_h[ $tre ] +[ $six ] ;
        the same as
        $ratio = $$d_h[ $tri ][ $six ] / $$d_h[ $tre ][ $six ] ;
        Thanks for your patience in coaching me so far..I really really appreciate it.