in reply to Re^13: 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 think we should hae done fewer columns ...

So next step would be, instead of doing  my $ratio1_1_1... (pencil and paper), write a function to create that array (like a real array not named scalars)

Which I did , I replicated your program using functions

What I did was try a few different nested loops (counter), and compare the numbers to yours (what you typed)

It took like 8 permutations to get the numbers to match

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; 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", 1+$six, 1+$tri, 1+$tre, $tri, $six, $tre, $six, ;;; $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] + / $$d_h[ $tre ][ $six ] ; } } } return \@ratio ; }

Naturally this is 0 indexed since programmers count from 0 (I was not tempted to change $ARRAY_BASE )

Then I turned more of your stuff into arrays slowly so that when writing functions I can compare/verify numbers .... should have done 2 rows 2 colums, whew :)

Now it wasn't too huge of a deal to compare what you got (what is expected) with what I got using my eyes, but once you come up with a function like I did, you should write a test for it, ...

 

Does this help you or would you like to see more?

Anything "click" for you after seeing makeRatio ? What clicked

Would you like to try writing the next function that makes use of \@ratio?

Or would you like to see the rest of my program (makeRatioAvg and makeFinal) ?

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

Replies are listed 'Best First'.
Re^15: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 18, 2014 at 01:08 UTC
    Hi! Ok. Been trying out writing the function similar to yours for past two days and I have a question here.

    I tried running the similar code on my script and I get the error message whereby it stated that

    can't use string ("0") as an ARRAY ref while "strict refs" in use at l +ine 119. Only hard reference allowed by "strict refs".
    SO I change the value here for this part (0 to 3) and I add in the main for loop:
    for my $dataratio(@full_data){ for my $six ( 3 .. $columns ){ for my $tre ( 3 .. $rows ){ for my $tri ( 3 .. $rows ){ } } }
    can't use string ("M11") as an ARRAY ref while "strict refs" in use at + line 119. Only hard reference allowed by "strict refs".
    Ok. i think I am doing something wrong here. So for my( $d_h ) = @_; how do you actually exclude out the first three columns of full_data (e.g. M11, M12..)? Another question is this code:
    1+$six, 1+$tri, 1+$tre, $tri, $six, $tre, $six, ;;; $ratio[ $six ][ $tri ][ $tre ] = $$d_h[ $tri ][ $six ] + / $$d_h[ $tre ][ $six ] ;
    I do not understand how this part works. Hope you can nudge me in the right direction?

      I do not understand how this part works. Hope you can nudge me in the right direction?

      If you'd be willing to be more specific I'd be willing to elaborate -- maybe you can explain which parts/characters you do understand and which parts you're hazy on

      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.

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

        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)