Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > $_ in condition

Reply
Thread Tools

$_ in condition

 
 
jason@cyberpine.com
Guest
Posts: n/a
 
      08-11-2004
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
 
Reply With Quote
 
 
 
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-11-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
jason@cyberpine.com
Guest
Posts: n/a
 
      08-12-2004
> 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.
 
Reply With Quote
 
jason@cyberpine.com
Guest
Posts: n/a
 
      08-12-2004
> 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.
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-12-2004
(E-Mail Removed) 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
 
Reply With Quote
 
Gunnar Hjalmarsson
Guest
Posts: n/a
 
      08-12-2004
(E-Mail Removed) 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
 
Reply With Quote
 
Joe Smith
Guest
Posts: n/a
 
      08-12-2004
(E-Mail Removed) 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
 
Reply With Quote
 
Jim Gibson
Guest
Posts: n/a
 
      08-12-2004
In article <(E-Mail Removed) >,
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with while condition OR condition Bill W. Ruby 13 05-09-2011 09:42 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM
while condition Daniel VHDL 7 05-23-2005 02:29 PM
one-hot encoding and fale-safe condition. Mohammed A khader VHDL 12 02-01-2005 04:24 PM
string condition file - lookup logic jason@cyberpine.com Perl 0 08-10-2004 07:28 PM



Advertisments