irrelevant characters match in PERL pattern matching
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
irrelevant characters match in PERL pattern matching
Hi,
I have some issue with the code of mine.
I see a strange behavior.
The pattern matching doesn't work correctly when my syntax is still perfect.
It matches irrelevant characters.
The problem goes like this:
The patterns are of the form ALLOCID_0xABCD.
I need to match this and i send the exact data [$argument here]to be matched.
When i run my code, i find something of the pattern "xyzbnbb.c" also to get match.
Below is the code,
&match_routine($prev_file,@ids);
sub match_routine {
open(FH,"$dir_path/$prev_file");
open(FW,">$dir_path/$prev_file.swp");
print " The file is $prev_file and the ids are @ids";
print "argument is $argument";
if ( $line =~ m/$argument/i ) {
print "content matched is $&\n argument sent is $agument\n";
}
}
}
close (FH);
close (FW);
}
Note the contents:
@ids=ALLOCID_0XABCD and so on.
$prev_file=abcd.cxx and similar files.
My aim is to open the file, match the pattern, and write something to the file.
Please find the output below.
Kindly check the lines print "argument is $argument" and "print "content matched is $&\n argument sent is $agument\n";"
" argument is ALLOCID_0x0503
content matched is ALLOCID_0x0503
argument sent is
.............
.......
......
argument is ALLOCID_0x0503
argument is ALLOCID_0x0504
content matched is ALLOCID_0x0504
argument sent is
I get output for the first one, yet the next print shows nothing.
How could this be so? Immediately after passing thro' a pattern check, how could a value vanish away?
Can somebody explain me where i'm going wrong.
Hi,
I have some issue with the code of mine.
I see a strange behavior.
The pattern matching doesn't work correctly when my syntax is still perfect.
It matches irrelevant characters.
The problem goes like this:
The patterns are of the form ALLOCID_0xABCD.
I need to match this and i send the exact data [$argument here]to be matched.
When i run my code, i find something of the pattern "xyzbnbb.c" also to get match.
Below is the code,
&match_routine($prev_file,@ids);
sub match_routine {
open(FH,"$dir_path/$prev_file");
open(FW,">$dir_path/$prev_file.swp");
print " The file is $prev_file and the ids are @ids";
print "argument is $argument";
if ( $line =~ m/$argument/i ) {
print "content matched is $&\n argument sent is $agument\n";
}
}
}
close (FH);
close (FW);
}
Note the contents:
@ids=ALLOCID_0XABCD and so on.
$prev_file=abcd.cxx and similar files.
My aim is to open the file, match the pattern, and write something to the file.
Please find the output below.
Kindly check the lines print "argument is $argument" and "print "content matched is $&\n argument sent is $agument\n";"
" argument is ALLOCID_0x0503
content matched is ALLOCID_0x0503
argument sent is
.............
.......
......
argument is ALLOCID_0x0503
argument is ALLOCID_0x0504
content matched is ALLOCID_0x0504
argument sent is
I get output for the first one, yet the next print shows nothing.
How could this be so? Immediately after passing thro' a pattern check, how could a value vanish away?
Can somebody explain me where i'm going wrong.
This line:
Code:
"content matched is $&\n argument sent is $agument\n";"
Needs to be checked. The first print is passing $&. The second is $agument. You've misspelled the variable name.
Thanks,i corrected that.
Yet please note that it matches irrelevant characters and again it is blank in the second output. I find it weird yet it happens.
The output is as below.
argument is ALLOCID_0x0604
argument is
content matched is ScannedMediaSimplexCount.c
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.