pramod has asked for the wisdom of the Perl Monks concerning the following question:
Hi,
I am trying to read a file containing the following strings: "Width = 32" and "Descr - This is Register1 comment"
and extract the Width = 32 from file, but my code fails to match.
File contains:
Width = 32 <uart0_rx_data : 16'h0000> Descr - "This is Register1 comment" # f_name bit_pos RESERVED 31:8 RXDATA 5:0 </uart0_rx_data>
Code snippet:
if($line =~ m/^Width[\s]*\=[\s]*\d+$/) # to match Width { update_curs ($line); } elsif ($line =~ m/^Descr[\s]*-[\s]*[\w]+$/) # to match Descr { update_curs ($line); } else { printf "Garbage found: \"%s\" \n",$line; }
Output: Garbage found: "Width = 32"
Kindly help.
UPDATE: Thanks. I have removed extra spaces and new line character in my code. When I run the code individually it works, but when integrated with main package it doesn't work. As tobyink mentioned "Descr" is not matched.
sub validate_save_regs { my $line = $_; chomp $line; if($line =~ m/^Width[\s]*\=[\s]*\d+$/){ $reg_width_found = 1; update_curs ($line); } elsif ($line =~ m/^Descr[\s]*-[\s]*[\w]+$/) { printf "inside elsif, line is %s\n",$line; if(!$reg_offset_found) { printf "Garbage found at: %d \n", $line_no; exit(0); } $name_found = 1; update_curs ($line); } else { printf "Garbage found %s\n", $line; } }
In this integrated code "Descr - This is Register1 comment" doesn't match and goes to Garbage rule.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: Regular Expression
by moritz (Cardinal) on Jun 27, 2012 at 11:52 UTC | |
Re: Regular Expression
by tobyink (Canon) on Jun 27, 2012 at 11:48 UTC | |
Re: Regular Expression
by roboticus (Chancellor) on Jun 27, 2012 at 13:26 UTC | |
by raaj (Acolyte) on Jun 28, 2012 at 09:32 UTC | |
Re: Regular Expression
by ckj (Chaplain) on Jun 27, 2012 at 12:00 UTC | |
by ww (Archbishop) on Jun 27, 2012 at 14:15 UTC | |
Re: Regular Expression
by prashantktyagi (Scribe) on Jun 27, 2012 at 11:47 UTC |
Back to
Seekers of Perl Wisdom