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

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

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