in reply to return a word next to the word you give
my $word = "color: "; my $lineno = 0; while (<STDIN>) { $lineno++; if (m#$word(\w+)#) { # $1 should now contain the color } }
There's no need to slurp the whole file into memory if you are sure that there will never be a linebreak between the color: and the actual color name.
Also, if you're sure that $word will never change its value, you should postfix the regexp with "o" for efficiency reasons:
if (m#$word(\w+)#o) {
but only if you're 100% sure that the contents of $word will not change!
Hope this helps.
Liz
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: return a word next to the word you give
by Abigail-II (Bishop) on Oct 16, 2003 at 12:32 UTC | |
by liz (Monsignor) on Oct 16, 2003 at 13:31 UTC |