in reply to Re^2: 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, Yes! I am interested. :)

Great, if you're willing, read Re: Perl MIME parser partially works with my code I wrote (code does not exist , there is no code)

Maybe also read some of the links it links ... maybe even these Tutorials: Variable Scoping in Perl: the basics,
Coping with Scoping , Mini-Tutorial: Perl's Memory Management,
Lexical scoping like a fox,

And then write one program that follows the the pattern ; try to make the changes I mentioned, and see how far you can get

Post the program, and we'll go from there

Might start like

#!/usr/bin/perl -- use strict; use warnings; use Data::Dump qw/ dd /; Main( @ARGV ); exit( 0 ); sub Main { my $frobnications = ...; # formerly \@full_data ... or DemoFrobnic +ations(); my( $DE, $FG ) = romulate_DE_FG( $frobnications ); dd( $DE ); dd( $FG ); my( $avgDE, $stddevDE ) = pancakes( $DE ); ## AddColumn( $target, $column ); AddColumn( $frobnications, ... ); } sub romulate_DE_FG { my( $frobnications ) = @_; my( @DE, @FG ); for my $row ( @$frobnications ){ ... push @DE, ... } return \@DE, \@FG; } sub AddColumn { my( $frobnications, $column ) = @_; ... } sub DemoFrobnications { return ...; ## output of dd( \@full_data ); }

Each time you start work on a new subroutine start a new file, even if you're using (version control);; If you're not using version control, DIY, save frobnicator-00.pl frobnicator-01.pl frobnicator-02.pl as you try things out/make changes ... its amazing what you can learn when you revisit your attempts weeks/months/years later

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

Replies are listed 'Best First'.
Re^4: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 12, 2014 at 01:19 UTC
    Great! Thanks! I am gonna look through it. I'll come back again if I have doubts to clarify :) Will post my code up once I manage to understand how to go from there
Re^4: How to store the output from foreach loop into variable/array without printing?
by hellohello1 (Sexton) on Mar 13, 2014 at 09:42 UTC
    Ok. I have read through some of the links you have given and already arrange my code properly. I'm gonna post the code here (in simplified terms) and show you what I have been doing and I suspect which line I am getting it wrong

    Here is part of my code (its the same as the one I post earlier but I change it to make it looks simpler to understand:

    $variable1 = ''; print OUT1 "$first[0]\t$first[1]\t$first[2]\t"; for my $index1 (3..8) { my $ratio1 = sprintf( "%.4f%s", $numerator/$denominator,"\t"); #print OUT1 "$ratio1"; $variable1 = "$ratio1"; # problem with this line } print OUT1 "$variable1"; # print to textfile print OUT1 "\n";

    I am trying to print out the output after it run finish the for loop 6 times (3 to 8). The data should arrange something like this:

    Desired output (e.g.):

    A98 0.98 123 4 4 4 4 4 4 A09 0.87 154 2 6 5 8 3 1 A12 0.12 873 6 1 2 4 7 0
    Instead, it print out only the last column:
    A98 0.98 123 4 A09 0.87 154 1 A12 0.12 873 0
    so I change to this line by adding the "." to join the 6 columns together
    $variable1 .= "$ratio1"; # problem with this line
    and I get weird output like this:
    A98 0.98 123 4 4 4 4 4 4 A98 0.98 123 4 4 4 4 4 4 2 6 A98 0.98 123 4 4 4 4 4 4 2 6 4 A98 0.98 123 4 4 4 4 4 4 2 6 4 6 A98 0.98 123 4 4 4 4 4 4 2 6 4 6 1 ...

    I know its got to do with the placement of the $variable1 .= "$ratio1"; ...but I have simply no idea how to go on and correct from there.

    The reason for attempting to capture the values from the for loop is because I want to calculate my average and CV. This variable will then put into hashes with the first three columns and go thru filter to filter out based on certain conditions.

      Instead, it print out only the last column:
      so I change to this line by adding the "." to join the 6 columns together and I get weird output like this:
      You're not actually using the index variable ($index1). And you also need to properly re-initialize $variable1.

      Would this do what you want?

      my @ratio = map { sprintf '%.4f', $numerator[$_]/$denominator[$_] } 3. +.8; my $variable1 = join "\t", @ratio;

      I know its got to do with the placement of the $variable1 .= "$ratio1"; ...but I have simply no idea how to go on and correct from there.

      Sounds like you're not Coping with Scoping (tutorial and diagnosis)

      If you can post more than a a fragment, something I can run (like your current short self contained program), I can explain what is going on

        Hi, ok. Here's my actual code:
        while(<CURINFILE>) { push @full_data , [split] while (<CURINFILE>); for $arr_ref1 (@full_data) { for $arr_ref2(@full_data) { print OUT1 "$$arr_ref1[0]\t$$arr_ref1[1]\t$$arr_ref1[2]\t\t"; #prin +t first three columns of input data into textfile $firstend = $firstinput + 2; $start2 = $firstend + 1; $secondend = $firstend + $secondinput; for my $index1 (3..$firstend)#column 4 to end of group 1 column { $ratio1 = sprintf( "%.4f%s", $$arr_ref2[$index1]/$$arr_ref1[$inde +x1],"\t"); print OUT1 "$ratio1"; #$variable1 .= "$ratio1"; #attempting to store values in $variabl +e1 } for my $index2 ($start2..$secondend)#start of group 2 column to +end) { $ratio2 = sprintf( "%.4f%s", $$arr_ref2[$index2]/$$arr_ref1[$ind +ex2],"\t"); print OUT1 "$ratio2"; #$variable2 .= "$ratio2"; #attempting to store values in $variab +le1 } print OUT1 "\n"; #print next line after one loop } # finish one loop of one ID }
        $arr_ref1(@full_data)and $arr_ref2(@full_data) refer to the same input of data from D to the end of data. Input DATA (Column letter is just to indicate my data)
        A: ID B, C (time) C to E: Group 1 F to H: Group 2 # Calculation is taking the ratio of each row per column _Input DATA_ A B C D E F G H M11 0.1 10 24 56 77 98 72 M12 0.3 13 44 23 45 56 11 M13 0.4 54 23 11 25 67 91

        What my code on top does is to output this:

        _OUTPUT DATA_ A B C D E F G H I M11 0.1 10 1 1 1 1 1 M12 0.3 13 1.83 0.41 0.58 0.57 0.15 M13 0.4 54 0.95 0.19 0.32 0.68 1.26 . . .
        Right now, I do not want to print the output out into textfile because I have huge dataset. So I am attempting to take output of the column E to I and put into variable and then be used to calculate the average by each group and row. That is why I put two groups (for loop) separately. After which, the data will be filtered and print out the desired output.

        Hence, I add the $variable1 and $variable2 to join the values of each cell together. But the output become like this:

        _OUTPUT DATA_ A B C D E F G H I J K M11 0.1 10 1 1 1 M11 0.1 10 1 1 1 1.83 0.95 M11 0.1 10 1 1 1 1.83 0.95 1 0.41 M11 0.1 10 1 1 1 1.83 0.95 1 0.41 . . .
        I am really stuck here. Thanks for your patience in guiding me so far!