469,287 Members | 2,357 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,287 developers. It's quick & easy.

$_ in condition

Pardon the perl noobie post, this one is silly one I'm sure, but
nonetheless, I'm stuck on it. why doesn't $_ substitute with the data
when the program is passed the string as such. The eventual objective
of the program being to test the string against a list of regexes.

perl test.pl 'xxxxxxxxxxx test1 xxxxxxxxxxxxxxx'

note commented line works!

Along the same lines, how could I pass regex lines in that same data
to condition that a string fed to the program does in fact match.

Many thanks.

#!/usr/bin/perl -w
use strict;
my $request = shift @ARGV;
print $request;
my $p='n';
while(<DATA>) {
print $_;
if ($request =~ $_)
# if ($request =~ 'test1')

{
$p='y';
}
}
if ($p =~ 'y')
{
print "\n Passed! \n";
}
else
{
print "\n Failed! \n";

}
__DATA__
test1
test2
test3
Jul 19 '05 #1
7 7152
ja***@cyberpine.com wrote:
why doesn't $_ substitute with the data
when the program is passed the string as such.
Substitute? Suppose you mean match.

The reason is that $_ includes a trailing "\n".

<snip>
while(<DATA>) {


chomp; # this should fix the problem

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #2
> chomp; # this should fix the problem

This did not appear to fix the issue, and actually the code only
appears to "pass" when I send it the last entry in the data.
$ perl m2.pl 'test2'
test2
Failed!
$ perl m2.pl 'test3'
test3
Passed!

The code I'm testing with:

#!/usr/bin/perl -w
use strict;
my $request = shift @ARGV;
print $request;
my $p='n';
while(<DATA>) {
chomp;
if ($request =~ $_)
{
$p='y';
}
}
if ($p =~ 'y')
{
print "\n Passed! \n";
}
else
{
print "\n Failed! \n";

}
__DATA__
test1
test2
test3
Thanks for any help or information.
Jul 19 '05 #3
> chomp; # this should fix the problem
Did not appear to fix the problem. And pardon the repost, but to add
further to my confusion on while loops, I made a few changes to the
this little program and added displays. Why doesnt my condition trip
and/or stay set for $p in the below code. Here are the three test
attempts. Also, note how $p displays on top of the first display
until the the last record - why is that????
$ perl m2.pl test2

p=xuest====test2 current====test1

p=xuest====test2 current====test2

request====test2 current====test3 p=x
$ perl m2.pl test3

p=xuest====test3 current====test1

p=xuest====test3 current====test2

request====test3 current====test3 p=y

Passed!
$ perl m2.pl test2

p=xuest====test2 current====test1

p=xuest====test2 current====test2

request====test2 current====test3 p=x
$
#!/usr/bin/perl -w
use strict;
my $request = shift @ARGV;
my $p='x';
while(<DATA>)
{
chomp;
if ($request =~ $_){$p='y';}
print "\n";
print ' request====';
print $request;
print ' current====';
print $_;
print ' p=';
print $p;
print "\n";
}
if ($p =~ 'y') {print "\n Passed! \n";}

__DATA__
test1
test2
test3
Thanks for any help or information.
Jul 19 '05 #4
ja***@cyberpine.com wrote:
chomp; # this should fix the problem


This did not appear to fix the issue, and actually the code only
appears to "pass" when I send it the last entry in the data.


Here's your program with better indenting, more perl idioms, and
using // with =~.

unix% cat temp.pl
#!/usr/bin/perl -w
use strict;
my $request = shift @ARGV;
my $p;
while(<DATA>) {
print "Checking '$request' for $_";
chomp;
$p = $. if $request =~ /$_/;
}
if ($p) {
print "'$request' matched line $p\n";
} else {
print "'$request' Failed!\n";
}
__DATA__
test1
test2
test3
unix% ./temp.pl test2
Checking 'test2' for test1
Checking 'test2' for test2
Checking 'test2' for test3
'test2' matched line 2
unix% ./temp.pl ::test3::
Checking '::test3::' for test1
Checking '::test3::' for test2
Checking '::test3::' for test3
'::test3::' matched line 3
unix% ./temp.pl test4
Checking 'test4' for test1
Checking 'test4' for test2
Checking 'test4' for test3
'test4' Failed!
unix% perl -v
This is perl, v5.8.3 built for sun4-solaris
Jul 19 '05 #5
ja***@cyberpine.com wrote:
Gunnar Hjalmarsson wrote:

chomp; # this should fix the problem
This did not appear to fix the issue, and actually the code only
appears to "pass" when I send it the last entry in the data.


It now works as intended for me.

<code snipped>
__DATA__
test1
test2
test3


Did you possibly have trailing spaces (besides the newlines) after
"test1" and "test2"?

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
Jul 19 '05 #6
ja***@cyberpine.com wrote:
Also, note how $p displays on top of the first display
until the the last record - why is that????
That's what happens when you move a file from Windows to Unix
and forget to use ASCII mode. It looks lik you've got
__DATA__
test1\r\n
test2\r\n
test3\n

while(<DATA>)
{
chomp;


while(<DATA>) {
chomp;
print "Unexpected carriage-return detected: '$_'\n" if /\r/;

I expect that it will print
'nexpected carriage-return detected: 'test1
with the closing single quote coming after "test1\r".

Better to strip leading and trailing whitespace.

while(<DATA>){
s/^\s*//;
s/\s*$//; # No need for chomp after doing this
$p = 'y' if $request =~ /\Q$_\E/;
}

-Joe
Jul 19 '05 #7
In article <ef**************************@posting.google.com >,
<ja***@cyberpine.com> wrote:
chomp; # this should fix the problem

Did not appear to fix the problem. And pardon the repost, but to add
further to my confusion on while loops, I made a few changes to the
this little program and added displays. Why doesnt my condition trip
and/or stay set for $p in the below code.


As you have already been told, you need to put slashes (/$_/) around
your regular expressions. People are not likely to help you if you
ignore their advice.
Here are the three test
attempts. Also, note how $p displays on top of the first display
until the the last record - why is that????


That will happen when you have a carriage return ("\r") in your string
without a line feed. Perhaps you are reading a file with DOS line
endings in a Unix environment that expects only line feeds.

And once again, this newsgroup is defunct. Try comp.lang.perl.misc in
the future.
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Kendal Goodrich | last post: by
3 posts views Thread by Ben R. | last post: by
3 posts views Thread by Yohan | last post: by
4 posts views Thread by joh12005 | last post: by
3 posts views Thread by jm.suresh | last post: by
reply views Thread by RN1 | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.