LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 08-21-2008, 03:44 AM   #1
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Rep: Reputation: 15
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";

while ($line = <FH>) {
#print FW $line;
foreach $argument (@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.
 
Old 08-21-2008, 08:55 AM   #2
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,636

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by gaynut View Post
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";

while ($line = <FH>) {
#print FW $line;
foreach $argument (@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.
 
Old 08-21-2008, 10:52 PM   #3
gaynut
LQ Newbie
 
Registered: Jan 2008
Posts: 27

Original Poster
Rep: Reputation: 15
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
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Perl pattern matching - greedy wondergirl Programming 2 06-17-2008 03:32 PM
Perl pattern matching in VB rigel_kent Programming 1 05-30-2006 11:00 AM
perl pattern match question lluciano Programming 4 02-28-2006 05:59 AM
Perl Pattern Matching Question pete1234 Programming 2 08-27-2005 10:26 AM
pattern matching in perl ludeKing Programming 9 04-02-2004 09:53 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration