Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Parsing in perl

by McDarren (Abbot)
on Jun 09, 2009 at 16:05 UTC ( #769979=note: print w/ replies, xml ) Need Help??


in reply to Parsing in perl

Hi :)

You're doing a lot of convulted stuff there with arrays and C-style loops, that really isn't necessary.

When you need to examine a file line by line, you're usually better reaching for a 'while' loop, rather than a 'for' loop.

Here is a slightly more perlish way to do what you want.

#!/usr/bin/perl use strict; use warnings; my $le = 'lethal_results.txt'; open my $in, '<', $le or die "Cannot open $le:$!\n"; while (<$in>) { chomp; my $foo = (split)[4]; print "$foo\n" if $foo >= 50; } close $in;
Of course, you could always just do something like:
perl -lane 'print $F[4] if $F[4] >= 50' < lethal_results.txt

Cheers,
Darren :)


Comment on Re: Parsing in perl
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (18)
As of 2015-07-28 14:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (256 votes), past polls