$_ in condition

J

jason

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
 
G

Gunnar Hjalmarsson

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".

while(<DATA>) {

chomp; # this should fix the problem
 
J

jason

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.
 
J

jason

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.
 
J

Joe Smith

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
 
G

Gunnar Hjalmarsson

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.

__DATA__
test1
test2
test3

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

Joe Smith

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
 
J

Jim Gibson

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.[/QUOTE]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top