Here is makeRatioTreAgain.pl , the fourth in a series, all are essentially makeRatio, all produce identical output on STDOUT

see me run the programs and check the identical-ness

perl makeRatioDebVisMat.pl >n-out-deb.txt perl makeRatioTriAgain.pl >n-out-tri.txt perl makeRatioTriAliasAgain.pl >n-out-ali.txt perl makeRatioTreAgain.pl >n-out-tre.txt $ md5sum n-out*txt 40b87695df5c3f839717dfb3538c029b *n-out-ali.txt 40b87695df5c3f839717dfb3538c029b *n-out-deb.txt 40b87695df5c3f839717dfb3538c029b *n-out-tre.txt 40b87695df5c3f839717dfb3538c029b *n-out-tri.txt

the basic idea of each is to do as little as possible in the body of a loop, and to give meaningful names to chunks of code :)(yes, TreMeaningful I know)

hopefully, by writing programs like this, you learn how each part works, what each part does ... practice makes perfect;;; best part, you can review/compare them later, refresh your recollection

cementing the logic, the program flow , in your brain, how/why/when the variable valuess change, this is the reason to have many named seperate files, to have many small named subroutines ... learning to think like a computer (and communicate that effectively to the computer) requires practice like this

actual program, download it, run it, study the output, add print statements ... become robot :)

#!/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 $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 ], ]; my $ratio_ref = makeRatioTreAgain( $d_h ); dd( $ratio_ref ); } sub makeRatioTreAgain { my( $d_h ) = @_; my $rows = $#$d_h; ## last index of array my $columns = $#{ $d_h->[0] }; ## last index of first row my @ratio; for my $six ( 0 .. $columns ) { TreAgain( $six, $rows, \@ratio, $d_h ); print "\n"; } return \@ratio; } ## end sub makeRatioTreAgain sub TreAgain { my( $six, $rows, $ratio_ref, $d_h ) = @_; for my $tre ( 0 .. $rows ) { ddebuggering( $ratio_ref ); TriAgain( $six, $tre, $rows, $ratio_ref, $d_h ); } } sub TriAgain { my( $six, $tre, $rows, $ratio, $d_h ) = @_; for my $tri ( 0 .. $rows ) { DebugVisualMatching( $six, $tri, $tre ); ## used to be #~ $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] / $$ +d_h[ $tre ][ $six ] ; ## can be written as #~ $ratio->[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] / +$$d_h[ $tre ][ $six ] ; ## or as $$ratio[$six][$tri][$tre] = $$d_h[$tri][$six] / $$d_h[$tre][$s +ix]; } } ## end sub TriAgain sub DebugVisualMatching { my( $six, $tri, $tre ) = @_; ## for visual matching printf 'my $ratio%d_%d_%d = $$d_h[%d][%d] / $$d_h[%d][%d];' . "\n +", 1 + $six, 1 + $tri, 1 + $tre, $tri, $six, $tre, $six, ; } sub ddebuggering { warn Data::Dump::pp( @_ ), "\n"; }

If you want I can post the others also

I can also post my original edit of makeRatio with accompanying makeRatioAvg and makeFinal (although only the single versions of these), but its probably better to play with makeRatioTreAgain.pl until you're sure you understand whats going on

Its like a mousetrap made from sticks and a rock -- you can try really hard and make one successfully, but if you want to change a few parts and make one hundred mousetraps, you really gotta understand why the simplest one worked first :)


In reply to Re^17: How to store the output from foreach loop into variable/array without printing? by Anonymous Monk
in thread How to store the output from foreach loop into variable/array without printing? by hellohello1

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.