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

Re: Detect Two Strings in File

by CountZero (Bishop)
on Nov 07, 2014 at 05:41 UTC ( [id://1106452]=note: print w/replies, xml ) Need Help??


in reply to Detect Two Strings in File

No loops and just one regular expressioon:
use Modern::Perl '2014'; my $first = qr/STRING/; my $second = qr/MAGIC/; my $corpus = join '', map { chomp; $_ } <DATA>; if ( $corpus =~ m/($first)(?!.*$first).*($second)(?!.*$first)/ ) { say "Success: $-[1] and $-[2]"; } else { say "Failure"; } __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? Yes, It's like MAGIC we are happy Ah, STRING and MAGIC success MAGIC and STRING failed
Fiddle around with the DATA-section to check the various possibilities and combiinations.

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 07, 2014 at 13:29 UTC
    Nice!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-18 18:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found