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

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

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
    Thanks for the useful links in your previous post. I have been reading them. I am still reading up on the doc that you post it here regarding about the array of array while referring to your code structure. Can I ask if I could also take a look at how you get the average of the ratio values as well?

    I would like to take the time to read it thru and then referred to your code as I learn better that way. I will post it here if I have problem understanding and also my code once I get the hang of it.

    Another question, when splitting the data (by tab since its tab delimited file), it will be split according to columns right (downwards). Is it possible to split by rows (horizontal)? Will that be easier for this kind of ratio thing or it doesn't make a difference?

      Can I ask if I could also take a look at how you get the average of the ratio values as well?

      Sure, here it is both makeRatioAvg and makeFinal

      As you might guess, makeRatioAvg and makeFinal is still me following closely the pencil and paper moves -- didn't want to get lost :) Now I have reference program with reference test data, so if I get ambitious and try to combine makeRatioAvg+makeFinal I will notice easily when I make mistake :)


      Another question, when splitting the data (by tab since its tab delimited file), it will be split according to columns right (downwards). Is it possible to split by rows (horizontal)? Will that be easier for this kind of ratio thing or it doesn't make a difference?

      I'm not quite sure what you mean, but I doubt it makes much of a difference, here is why

      rows are lines ... each lines consists of tab seperated values (columns of a row) , so you're already doing what is possible in the most straight forward way

      the only issue I saw with your code is you had "while while" loop, where the outer while was used just for the header ... I wrote that as

      It was called from DoWorkOriginal

      Where sub monkeyBusiness is the foreach loops from your code which I didn't understand

      The general strategy when I don't understand why/how something works, before I can begin to debug/improve, is to isolate/sequester everything into subroutines, and make sure it produces same output as original

        Thank you :) I am a very slow learner, so it will take me quite a while for me to digest and reply you.

        Before that, I want to clarify something really noob:

        my( $infh ) = @_;
        @_ refer to the whole dataset right? But if I want to only include data from column 4 onward to the end, I have to write a function to split the data and then take column 4 to end and put it into @_?