in reply to Re^16: 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?
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 :)
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^18: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 21, 2014 at 05:45 UTC | |
by Anonymous Monk on Mar 21, 2014 at 07:21 UTC | |
by hellohello1 (Sexton) on Mar 21, 2014 at 08:38 UTC | |
by Anonymous Monk on Mar 21, 2014 at 08:54 UTC | |
by hellohello1 (Sexton) on Mar 24, 2014 at 01:41 UTC | |
by hellohello1 (Sexton) on Mar 25, 2014 at 09:22 UTC | |
|