473,769 Members | 6,208 Online
Bytes | Software Development & Data Engineering Community
+ 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_srnam e",
nodetype => "node_srtyp e",
area => "node_lata" ,
cardtype => "srcardtype ");
$fltr_num_ops{E QUAL} = " == ";
$fltr_num_ops{N OTEQUAL} = " != ";
$fltr_num_ops{L IKE} = " =~ m// ";
$fltr_num_ops{N OTLIKE} = " !~ m// ";
$fltr_str_ops{E QUAL} = " eq ";
$fltr_str_ops{N OTEQUAL} = " ne ";
$fltr_str_ops{L IKE} = " =~ m// ";
$fltr_str_ops{N OTLIKE} = " !~ 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|$f ltr_op1|$fltr_v al1|$fltr_attr2 |$fltr_op2|$flt r_val2|$fltr_at t
r3|$fltr_op3|$f ltr_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 2685
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
11794
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 files A, and B each containing say 100,000 lines (each line=one string without any space) I want to do
3
2388
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: An expression a = b() + c() * d++; can be transformed with the rules of operator associativity and operator precedence into a tree
16
27923
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 own LAN, the driver tells me it cannot make the connection. Here are the ODBC driver connection parms: Data Source Name: (free field - name my "my linux box" will do nicely) Host/Server Name (or IP) - something like MSQLUserName@ServerName.net...
3
3290
by: David Isaac | last post by:
What's the standard replacement for the obsolete grep module? Thanks, Alan Isaac
5
3230
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 2+2, the value 4 *is produced*. Nothing *happens*. Thus, 4 is the value of the expression, and it has no side effects. In the expression g=2.0, the value 2.0 is produced. What *happens* is that 2.0 is assigned to g. Thus, 2.0 is the value of the...
8
4756
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 least)...the subject is "4 GB hard constraint on a Solaris 8 server". I figured I'd post over here because we aren't getting anywhere too fast over there, and I think it is a code issue. We have a piece of software that we purchased. It is a...
6
4917
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++ world? Is it like something that is not desired but happens automatically and we cannot prevent it? Would be great if someone throws light on this. Thanks,
13
10130
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 could be even better. But I have some problem with writing performance grep analog. It's my script:
47
3447
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): row+="a"
0
9587
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9423
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10211
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9993
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9863
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8870
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6672
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3958
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 we have to send another system
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.