in reply to Detect Two Strings in File
To test the edge case, delete the last line of the DATA-section.use Modern::Perl '2014'; my $first = qr/STRING/; my $second = qr/MAGIC/; my $first_pos; my $second_pos; while (<DATA>) { $first_pos = $. if /$first/; $second_pos = $. if /$second/; } if ( $first_pos and $second_pos > $first_pos ) { say "Success! STRING at $first_pos followed by MAGIC at $second_po +s"; } else { say "Failure! (STRING: $first_pos - MAGIC: $second_pos)"; } __DATA__ First line second line here is the STRING empty text the STRING again! followed by the MAGIC word more emptiness Oh no! the first STRING again sadness did we look in vain? Ah, MAGIC success
I have assumed that the first and second words cannot happen in the same line.
CountZero
A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James
My blog: Imperial Deltronics
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Detect Two Strings in File
by omegaweaponZ (Beadle) on Nov 06, 2014 at 14:47 UTC | |
by CountZero (Bishop) on Nov 07, 2014 at 05:03 UTC |