473,385 Members | 2,028 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

$_ 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 7232
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Kendal Goodrich | last post by:
In the setup project I am trying to create, I am wanting to search to see if DirectX 8 is installed on the local machine. I figured the best way to determine would be a registry key search, so I...
1
by: Joel | last post by:
As my registry start condition was working fine, now it's not working anymore Here are the kind of parameter I use in VSNEt2003: A) Registry : Name : Pipo; Property : PROP1; RegKey...
3
by: Ben R. | last post by:
In an article I was reading (http://www.ftponline.com/vsm/2005_06/magazine/columns/desktopdeveloper/), I read the following: "The ending condition of a VB.NET for loop is evaluated only once,...
3
by: Yohan | last post by:
Hello, I have a question concerning the template classes and their parameters. Is it possible to set a condition on the template parameters in a way that could block the compilation if the...
5
by: das | last post by:
hello all, this might be simple: I populate a temp table based on a condition from another table: select @condition = condition from table1 where id=1 in my stored procedure I want to do...
4
by: joh12005 | last post by:
Hello, i posted for suggestions a little idea even if it still needs further thoughts but as i'm sure you could help :) if would like to implement some kind of Condition class which i coud...
16
by: Claudio Grondi | last post by:
Sometimes it is known in advance, that the time spent in a loop will be in order of minutes or even hours, so it makes sense to optimize each element in the loop to make it run faster. One of...
3
by: jm.suresh | last post by:
Hi, I have a list and I want to find the first element that meets a condition. I do not want to use 'filter', because I want to come out of the iteration as soon as the first element is found. I...
4
by: Jane T | last post by:
I appreciate how difficult it is to resolve a problem without all the information but maybe someone has come across a similar problem. I have an 'extract' table which has 1853 rows when I ask for...
0
by: RN1 | last post by:
I have a DataGrid with an EditCommandColumn. In the EditCommand sub of the DataGrid, there is an If condition. This If condition is True for some rows in the DataGrid whereas it is False for the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.