Beefy Boxes and Bandwidth Generously Provided by pair Networks RobOMonk
Perl Monk, Perl Meditation
 
PerlMonks  

Perl starter with questions about processing data from a txt file

by willperl (Initiate)
on Jan 17, 2013 at 16:40 UTC ( #1013815=perlquestion: print w/ replies, xml ) Need Help??
willperl has asked for the wisdom of the Perl Monks concerning the following question:

Hi all: I am new to Perl and having some problems... I have a txt file that contains data like this.
A B C D 1.0E-1 2.0E-2 3.1E-3 3.0E-9 1.0E-1 2.0E-2 3.2E-3 3.0E-9 1.0E-1 2.0E-2 3.3E-3 3.0E-9 1.0E-1 2.0E-2 3.4E-3 1.0E-1
A B C D are separated by tab. I would like Perl to capture the C value right before D is larger than 1e-3 (capture Cn-1 when Dn > 1e-3; in this case that would be 3.3E-3), and my code right now looks like this:
open (linedata, "/test.txt"); while (<linedata>) { ($A, $B, $C, $D) = split(/\t/, $_); print "$A, $B, $C, $D, \n" } close (linedata);
I don't really know how to process the "capturing Cn-1" part... Would someone please give me some insight? And if I add chomp to my code, the program whined about doesn't return a chomp value, which I don't know the reason. Appreciate to have some insight on chomp, too. Thank you guys!

Comment on Perl starter with questions about processing data from a txt file
Select or Download Code
Re: Perl starter with questions about processing data from a txt file
by choroba (Abbot) on Jan 17, 2013 at 16:50 UTC
    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]; }
    لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

      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.
        لսႽ ᥲᥒ⚪⟊Ⴙᘓᖇ Ꮅᘓᖇ⎱ Ⴙᥲ𝇋ƙᘓᖇ

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (13)
As of 2014-04-17 08:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (441 votes), past polls