in reply to Re^15: 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. 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.
  • Comment on Re^16: How to store the output from foreach loop into variable/array without printing?
  • Select or Download Code

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

    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

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

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

      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

Re^17: How to store the output from foreach loop into variable/array without printing?
by Anonymous Monk on Mar 18, 2014 at 10:18 UTC

    Hi ok ... $d_h refers ...

    ok, good so far

    I am looping through 6 columns ($six), and in each column, I will do ratio against two values ($tre and $tri).

    And here is the problem, no ratio calculation is performed on the lines starting with printf, only a string is printed (printf prints a formatted string)

    This tells me you did not call makeRatio (did not copy/paste makeRatio after UsedToBeJustDoWork and invoke makeRatio from within UsedToBeJustDoWork

    Please do this and see what gets printed on stdout ... you should recognize what gets printed on stdout ... makeRatio replicates part of UsedToBeJustDoWork


    So may I know if there is any article that explain that or..?

    Well, perlintro and perlop and printf/sprintf

    The free book Modern Perl (or http://learn.perl.org/books/beginning-perl/) might be easier to understand than perlop

    If you use ppi_dumper it can help you understand the parts of a program. Here is the output of that portion

    So you see a "," comma does not terminate a statement, a statement is terminated by semicolon ";" which means all the symbols seperated by the comma operator are arguments to printf function


    And is $ratio... the same as $ratio ...

    I think you will have to tell me if they're the same :) Start a new file and employ ddumperBasic debugging checklist and compare the output you get (lesson courtesy of Basic debugging checklist and brian's Guide to Solving Any Perl Problem )

    The books teach more about arrays and arrays of arrays, as does perldsc ...

    You should spend more time with perlintro esp Perl variable types. By "spend time" I mean read it, type up at least ten ddumpering() programs , to learn what its trying to teach you, and equally important, to learn how to interpret ddumper output . The creatingfiles/typing part is important , babysteps (learning to crawl before you learn karate)

      The creatingfiles/typing part is important , babysteps (learning to crawl before you learn karate)

      Also, don't be discouraged :)

      You are good with the pencil and paper approach, you have a pretty good grasp of the problem, you're 80% finished :)

      Now all that is left is to gain confidence in the perl syntax, to better understand what your program is doing (what buckets its filling..), so you can tell the computer how to pencil in the answers for you