Problems? Is your data what you think it is? | |
PerlMonks |
in reply to Re: Matching a string in a parenthesized block (regex help)
in thread Matching a string in a parenthesized block (regex help)
I tried implementing your solution here:
use warnings; use strict; my $file = "/path/to/file.txt"; sub has_word { my $arg = $_[0]; local $/; open FILE, '<', $file; while ( <FILE> ) { if ( /^ASDF_$arg/ && /magic/ ) { close FILE; return 1; } else { close FILE; return 0; } } } sub main { if (has_word("ONE")) { print "ONE already has the word.\n"; } else { print "ONE does not have the word.\n"; } if (has_word("TWO")) { print "TWO already has the word.\n"; } else { print "TWO does not have the word.\n"; } } main;
Content of file in this particular case:
ASDF_ONE { magic tmp tmp } ASDF_TWO { tmp magic tmp } string3 { tmp tmp magic }
The output is not what I expect:
ONE already has the word. TWO does not have the word.
Indeed, all the sections in this case have the word.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^3: Matching a string in a parenthesized block (regex help)
by LanX (Saint) on Mar 06, 2021 at 22:48 UTC | |
Re^3: Matching a string in a parenthesized block (regex help)
by jwkrahn (Abbot) on Mar 06, 2021 at 22:04 UTC |