Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^2: Reading file and matching lines

by Jalcock501 (Sexton)
on Feb 11, 2014 at 13:26 UTC ( #1074416=note: print w/ replies, xml ) Need Help??


in reply to Re: Reading file and matching lines
in thread Reading file and matching lines

Apologies, Yes there are several instances of this in a single file, so I need to do this through out the file and only report any errors if there are any. If none the script should exit normally.


Comment on Re^2: Reading file and matching lines
Re^3: Reading file and matching lines
by karlgoethebier (Curate) on Feb 11, 2014 at 13:45 UTC
    "...only report any errors if there are any"

    If so, why not something simple like this:

    while (<IN>) { print qq($1\n) if /^(E)/; print qq($1\n) if /^(G)/; die $1 if /^(h)/; }

    Or do i still misunderstand the specs?

    Best regards, Karl

    «The Crux of the Biscuit is the Apostrophe»

Re^3: Reading file and matching lines
by GotToBTru (Chaplain) on Feb 11, 2014 at 14:23 UTC
    use strict; use warnings; my $infile = shift; my $found_E = 0; my $sets = 0; open my $ifh, '<', $infile; while(<$ifh>) { if (/^E/) { $found_E = 1; next; } if ($found_E) { if (/^G/) { $sets += 1; $found_E = 0; next; } if (/^h/) { print "Error! Found h before G\n"; exit; } } } close($ifh); printf "Found %d sets from E to G uninterrupted by h\n",$sets;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (6)
As of 2014-08-20 11:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (111 votes), past polls