Q: Why does this match not work?

J

jake1138

Can anyone explain this to me?

This works:

while(<FILE>) {
chop;
if (/^int config_write\s*\(.*\)\s*[^;]?\s*$/i) {
print "$_\n";
}
if (/^int config_read\s*\(.*\)\s*[^;]?\s*$/i) {
print "$_\n";
}
}

But this doesn't (only matches the first string "int_config_read"):

while(<FILE>) {
chop;
for my $func_def ("int config_read", "int config_write") {
if (/^$func_def\s*\(.*\)\s*[^;]?\s*$/i) {
print "$_\n";
}
}
}

If I reverse the strings...

for my $func_def ("int config_write", "int config_read") {

....it matches "int config_write" and not "int config_read".

I'm really confused because it does indeed process both strings if I
use code like this:

for my $func_def ("int config_read", "int config_write") {
print "$func_def\n";
}
 
A

Anno Siegel

jake1138 said:
Can anyone explain this to me?
[...]

But this doesn't (only matches the first string "int_config_read"):

while(<FILE>) {
chop;
for my $func_def ("int config_read", "int config_write") {
if (/^$func_def\s*\(.*\)\s*[^;]?\s*$/i) {
print "$_\n";
}
}
}

Something else is going on. It works as intended for me. Check the
input data.

Anno
 
G

Gunnar Hjalmarsson

Anno said:
jake1138 said:
But this doesn't (only matches the first string "int_config_read"):

while(<FILE>) {
chop;
for my $func_def ("int config_read", "int config_write") {
if (/^$func_def\s*\(.*\)\s*[^;]?\s*$/i) {
print "$_\n";
}
}
}

Something else is going on. It works as intended for me.

For me too.

But why not just

while (<FILE>) {
print if /^int config_(?:read|write)\s*\(.*\)\s*[^;]?\s*$/i
}
 
J

jake1138

Doh! The problem was elsewhere. Below is the full corrected code.
Before, I was opening the file once outside the "for my" loop and
closing it within the "for my" loop, thus <FILE> was empty when I got
to the second string.

for my $func_def ("int config_read", "int config_write") {
$str_protos .= "$func_def(struct x_config *config);\n";
open FILE, "$fname" or die "ERROR: Cannot open file '$fname'";
while(<FILE>) {
chop;
if ((/^$func_def\s*\(.*\)\s*[^;]?\s*$/i) || ($found == 1)) {
$found = 1;
$str_funcs .= "$_\n";
}
if ((/^\}$/i) && ($found == 1)) {
$str_funcs .= "\n";
last;
}
close FILE;
if ($found == 0) {
# generate the function code
}
found = 0;
}
}
 
T

Tad McClellan

jake1138 said:
while(<FILE>) {
chop;


That was how we removed newlines 8 years ago.

Where have you been? :)


The modern way to remove newlines is:

perldoc -f chomp
 
J

jake1138

Tad said:
That was how we removed newlines 8 years ago.

Where have you been? :)

The modern way to remove newlines is:

perldoc -f chomp

Good point. I copy and pasted someone else's code, plus I'm fairly new
to perl. So that's my excuse and I'm sticking to it! :)
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top