Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Perl starter with questions about processing data from a txt file

by choroba (Abbot)
on Jan 17, 2013 at 16:50 UTC ( #1013816=note: print w/ replies, xml ) Need Help??


in reply to Perl starter with questions about processing data from a txt file

Just remember the C value everytime, but only report it when needed:

#!/usr/bin/perl use warnings; use strict; open my $IN, '<', 'test.txt' or die $!; my $remember; while (<$IN>) { my @columns = split /\t/; print "$remember\n" if $columns[3] > 1e-3; $remember = $columns[2]; }
لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ


Comment on Re: Perl starter with questions about processing data from a txt file
Download Code
Re^2: Perl starter with questions about processing data from a txt file
by willperl (Initiate) on Jan 17, 2013 at 17:29 UTC

    Thanks! it worked but I am now confused. The code really listed the C value before D > 1e-3. But if I am understanding the code right, shouldn't Perl only report C values when D is > 1e-3 (that being said, I get Cn value instead of Cn-1)?

    And if I have multiple D values larger than 1e-3, can I ask perl to report only the first Cn-1 value to me, instead of listing all?

    Thanks again for any insights or answers!
      The code reports the remembered value when D > 1e-3, to the actual C value. As it is, it will report all the values. If you want to report only the first one, just exit the loop via last after printing a value. If you want to skip values that are on the neighbouring lines, just set a flag once you output a value, clear it otherwise, and only report a value if the flag is not set.
      لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ
        Thanks choroba! I played with the code and understand what you meant by saying "remembered" value. Still trying the "last" perl function but when I added it instead of giving me only one result my code returned nothing... Thanks again for your help!
        Oops~~ I added some conditions after the last command and was able to exit the while loop using last as recommended. Thanks again choroba!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (12)
As of 2014-10-20 09:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (74 votes), past polls