Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re^2: How to store the output from foreach loop into variable/array without printing?

by hellohello1 (Sexton)
on Mar 11, 2014 at 08:16 UTC ( [id://1077805]=note: print w/replies, xml ) Need Help??


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

Replies are listed 'Best First'.
Re^3: How to store the output from foreach loop into variable/array without printing?
by Anonymous Monk on Mar 11, 2014 at 11:12 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
      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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1077805]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (2)
As of 2024-04-26 00:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found