473,466 Members | 1,296 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

s/// has apparent side effect on grep()

Hi,

Problem:
Executing 's///' has a side effect on grep null string matching.
If line 62, the substitution, is executed the last two values returned by
grep and printed on lines 68, 69 are different than the values returned and
printed when line 62 is commented out. Line 62 shouldn't have any impact on
lines 67,68 & 69.

Environment:
(1) Reproducable under Perl 5.6.1 running on Linux 2.4.5 (Slackware 8.0
installation) Pentium MMX/166MHz, 96MB Ram.
(2) Reproducable under Perl 5.8.0 running on Linux 2.6.5 (Slackware 9.1.0
installation) AMD Athlon 64 3000+, 1GB Ram

To reply by e-mail, remove the dot-baseball treat-dot from my e-mail address

Example Code:
#!/usr/bin/perl -w
my $item;
my $fltr_expr;
my $fltr_attr1;
my $fltr_op1;
my $fltr_val1;
my $fltr_attr2;
my $fltr_op2;
my $fltr_val2;
my $fltr_attr3;
my $fltr_op3;
my $fltr_val3;
my @test_data = ();
my %fltr_num_ops;
my %fltr_str_ops;
my %card_columns = (network => "network_srname",
node => "node_srname",
nodetype => "node_srtype",
area => "node_lata",
cardtype => "srcardtype");
$fltr_num_ops{EQUAL} = " == ";
$fltr_num_ops{NOTEQUAL} = " != ";
$fltr_num_ops{LIKE} = " =~ m// ";
$fltr_num_ops{NOTLIKE} = " !~ m// ";
$fltr_str_ops{EQUAL} = " eq ";
$fltr_str_ops{NOTEQUAL} = " ne ";
$fltr_str_ops{LIKE} = " =~ m// ";
$fltr_str_ops{NOTLIKE} = " !~ m// ";

#------------------------------------------------------------------------
# Test data normally retrieved from database. This accurately represents
# the problem though.
#------------------------------------------------------------------------
push(@test_data, "cardtype|LIKE|HUB|||||||");
push(@test_data, "cardtype|EQUAL|CC2|||||||");
foreach $item (@test_data) {
( $fltr_attr1, $fltr_op1, $fltr_val1, $fltr_attr2, $fltr_op2, $fltr_val2,
$fltr_attr3, $fltr_op3, $fltr_val3 ) = split(/\|/, $item);

$fltr_attr1 =~ tr/A-Z/a-z/;
$fltr_attr1 =~ s#^\s*(\S*)\s*$#$1#;
$fltr_op1 =~ s#^\s*(\S*)\s*$#$1#;
$fltr_val1 =~ s#^\s*(\S*)\s*$#$1#;

#---------------------------------------------------------------------
# $fltr_attr2, $fltr_op2, fltr_val2, $fltr_attr3, $fltr_op3, fltr_val3
# are not processed in this example - normally they would've been.
#---------------------------------------------------------------------
$fltr_expr = "";
if( $fltr_val1 =~ m#^\d+$# ) {
$fltr_expr = qq{$fltr_attr1 $fltr_num_ops{$fltr_op1} $fltr_val1}; }
else {
$fltr_expr = qq{$fltr_attr1 $fltr_str_ops{$fltr_op1} "$fltr_val1"}; }

#---------------------------------------------------------------------
# This is where the problem is. Try (un)commenting out the
# middle line (substitution). It changes the results of the grep()s
# in the next section.
#---------------------------------------------------------------------
print "Before Substitute: '$fltr_expr'\n";
# $fltr_expr =~ s#m// +"*([^"]+)"*#m/$1/#g;
print "After Substitute: '$fltr_expr'\n";

print
"$fltr_attr1|$fltr_op1|$fltr_val1|$fltr_attr2|$flt r_op2|$fltr_val2|$fltr_att
r3|$fltr_op3|$fltr_val3\n";

print sprintf("grep(/$fltr_attr1/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr1/, keys %card_columns)));
print sprintf("grep(/$fltr_attr2/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr2/, keys %card_columns)));
print sprintf("grep(/$fltr_attr3/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr3/, keys %card_columns)));
print "fltr_attr1 = '$fltr_attr1'\n";
print "fltr_op1 = '$fltr_op1'\n";
print "fltr_val1 = '$fltr_val1'\n";
print "fltr_attr2 = '$fltr_attr2'\n";
print "fltr_op2 = '$fltr_op2'\n";
print "fltr_val2 = '$fltr_val2'\n";
print "fltr_attr3 = '$fltr_attr3'\n";
print "fltr_op3 = '$fltr_op3'\n";
print "fltr_val3 = '$fltr_val3'\n";
foreach (keys %card_columns) {
print "'$_' = '$card_columns{$_}'\n"; }
print "\n";
}
Jul 19 '05 #1
2 2659
John E. Jardine wrote:
Executing 's///' has a side effect on grep null string matching.


That is correct. It is documented behavior.

If the PATTERN evaluates to the empty string, the last success-
fully matched regular expression is used instead.

You need to change lines 67-69 in your program to

printf("grep(/$fltr_attr1/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr1/, keys %card_columns))) if $fltr_addr1 ne "";
printf("grep(/$fltr_attr2/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr2/, keys %card_columns))) if $fltr_addr2 ne "";
printf("grep(/$fltr_attr3/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr3/, keys %card_columns))) if $fltr_addr3 ne "";

-Joe
Jul 19 '05 #2
Hi Joe,

My bad - I must have missed that and simply never hit the issue before.
Thanks for clearing it up for me.

Cheers,
J.J.

On Mon, 12 Apr 2004, Joe Smith wrote:
John E. Jardine wrote:
Executing 's///' has a side effect on grep null string matching.


That is correct. It is documented behavior.

If the PATTERN evaluates to the empty string, the last success-
fully matched regular expression is used instead.

You need to change lines 67-69 in your program to

printf("grep(/$fltr_attr1/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr1/, keys %card_columns))) if $fltr_addr1 ne "";
printf("grep(/$fltr_attr2/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr2/, keys %card_columns))) if $fltr_addr2 ne "";
printf("grep(/$fltr_attr3/, keys %%card_columns) = %d\n",
scalar(grep(/$fltr_attr3/, keys %card_columns))) if $fltr_addr3 ne "";

-Joe

Jul 19 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
by: sf | last post by:
Just started thinking about learning python. Is there any place where I can get some free examples, especially for following kind of problem ( it must be trivial for those using python) I have...
3
by: Sensorflo | last post by:
After browsing though many newsgroups articels I'm still not shure how operator precedence, operator associativity, sequence points, side effects go together. Currently I have the following view: ...
16
by: MLH | last post by:
Using MS Access, I have attached to MySQL servers in other states and other countries on the other side of my router. But when I use the MySQL ODBC driver 3.51 to connect to a MySQL server on my...
3
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
5
by: Niklaus | last post by:
This is one of the posts that i got. ------------------------------ A "side effect" of an operation is something that *happens*, not something that *is produced*. Examples: In the expression...
8
by: clsmyth | last post by:
Folks, Hi, I have never posted to a language group before so please excuse me if this is inappropriate. I have posted this to comp.unix.solaris (well, I am one of the folks on the thread at...
6
by: Senthil | last post by:
Hi, Whenever i read a C++ book or a newsgroup posting, i come across the terms like " before the side effects completes" , "destructor with side effects" etc. What is this side effect mean in C++...
13
by: Anton Slesarev | last post by:
I've read great paper about generators: http://www.dabeaz.com/generators/index.html Author say that it's easy to write analog of common linux tools such as awk,grep etc. He say that performance...
47
by: Henning_Thornblad | last post by:
What can be the cause of the large difference between re.search and grep? This script takes about 5 min to run on my computer: #!/usr/bin/env python import re row="" for a in range(156000):...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.