19 v(in) voltage
23 v(in.in) voltage
I want to find the first line (above) by searching on "v(in)". If I search on "in" I might get the wrong result since one line(the second one) has "in.in".
in my perl script I have a variable who's value is:
$my_variable = "v(in)";
when I go through the file and search for the line shown above by using:
open(IN,'<myfile.txt');
while ($_ =<IN>))
if ($_ =~ $my_variable))
{print "I found it\n"}
close(IN);
I can't seem to find it. What's wrong?
ANy help appreciated.
Thanks,
Paul
[please post in plain text only!]
>
> in my perl script I have a variable who's value is:
> $my_variable = "v(in)";
>
> when I go through the file and search for the line shown above by
> using:
> open(IN,'<myfile.txt');
Get in the habit of checking for errors on open() and friends.
> while ($_ =<IN>))
^
This is overly verbose and wrong. Cut and paste next time, don't
re-write...
> if ($_ =~ $my_variable))
^
This is also wrong. Do you like parens or what?
> {print "I found it\n"}
> close(IN);
>
open(IN,'<myfile.txt') or die "can't open: $!";
while (<IN>)
{
if (/$my_variable/)
{
print "I found it\n"
}
}
close IN;
Hi Drew,
Thanks for your help. But, the problem seems to have something to do
with searching for text that has parentesis in it. The variable I am
searching for is 'v(in)' ala
$my_variable = "v(in)";
Then, if I use (as you suggest)....
if (/$my_variable/)
the if statement cannot seem to find the line of text that looks like
this....
19 v(in) voltage
Are the parentesis causing something unexpected (at least, unexpected to
me!) to happen??
Thanks!
Paul
> $my_variable = "v(in)";
>
> Then, if I use (as you suggest)....
>
> if (/$my_variable/)
>
> the if statement cannot seem to find the line of text that looks like
> this....
>
> 19 v(in) voltage
>
> Are the parentesis causing something unexpected (at least, unexpected to
> me!) to happen??
Yes, they do. Parens are special characters in regexes. For such case
use the \E modifier:
if (/\E$my_variable\Q/)
where the \Q is actually optional here.
This will take any character inside the pattern verbosely and not treat
them as special.
Tassilo
--
$_=q!subJust{another()};subanother{Perl()};subPerl{hacker()};subhacker{map
{($_=(caller(3-$_))[3])=~s/main:://;$_}0..3}print"@{[Just()]},"!;s/su
b/(reverse"bus").chr(32)/xge;tr~\n~~d;eval;
[ snip 40 lines ]
>Hi Drew,
Please do not quote an entire article. Trim it and leave just
enough to establish the context for you comments.
>But, the problem seems to have something to do
>with searching for text that has parentesis in it. The variable I am
>searching for is 'v(in)' ala
>
>$my_variable = "v(in)";
>
>Then, if I use (as you suggest)....
>
> if (/$my_variable/)
if (/\Q$my_variable/) # or: $my_variable = quotemeta $my_variable;
>Are the parentesis causing something unexpected (at least, unexpected to
>me!) to happen??
Yes, parenthesis are special in regular expressions.
perldoc perlre
--
Tad McClellan SGML consulting
ta...@augustmail.com Perl programming
Fort Worth, Texas
>Yes, they do. Parens are special characters in regexes. For such case
>use the \E modifier:
>
> if (/\E$my_variable\Q/)
The other way around:
if (/\Q$my_variable\E/)
>> if (/\E$my_variable\Q/)
>
>
> The other way around:
>
> if (/\Q$my_variable\E/)
Hmmh, must be a virus tormenting Germany remembering Find::File in a
previous post not long ago. :-)
The "\Q" definitely did the trick!!
Thanks again,
Paul
>>> if (/\E$my_variable\Q/)
>>
>> The other way around:
>>
>> if (/\Q$my_variable\E/)
> Hmmh, must be a virus tormenting Germany remembering Find::File in a
> previous post not long ago. :-)
and me saying use /^0?[1-9]+$/ for recognizing integers
withor without a leading zero... =)
a heard the flu is coming to germany...
--
http://tinita.de \ enter__| |__the___ _ _ ___
PerlQuotes: http://tinita.de/pq.html \ / _` / _ \/ _ \ '_(_-< of
MovieDB: http://tinita.de/tmdb.html \ \ _,_\ __/\ __/_| /__/ perception