Beefy Boxes and Bandwidth Generously Provided by pair Networks Joe
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Extracting pattern from a file

by ghosh123 (Scribe)
on Apr 17, 2012 at 05:25 UTC ( #965436=perlquestion: print w/ replies, xml ) Need Help??
ghosh123 has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monk, I have a file of the following content and structure:

=============================================== // 1st commented line //second commented line // timescale 1ns /5 ns timescale 1ns/4ns //this comment should also be ignored module modulename1 (a b c ) some lines.... some lines ... //third commented line //fourth commented line timescale 3ns/5ns module modulename2 (p q r) some lines ... //fifth comment //timescale 4ns/5ns timescale 1ns/3ns module modulename3 (m n p ) some line ... ================================================

In this above file structure '//' is used for commenting a line which will be ignored for parsing. I need to parse this above file so that I can extract the timescale value corresponding to each modulename. For example, modulename1 has 1ns/5ns as its timescale, modulename2 has 3ns/5ns and so on. Also the comments after the timescale value will be ignored as mentioned above. The problem being is the timescale value for a particular module is appearing above the module declaration in the file. Please help me in getting the timescale value corresponding to each modulename. Thanks.

Comment on Extracting pattern from a file
Download Code
Re: Extracting pattern from a file
by Ratazong (Prior) on Apr 17, 2012 at 06:03 UTC

    Hi gosh123,

    Your problem can be solved quite easily. Please have a look at the following pseudocode:

    my lastFoundTimescale = "undefined"; # declare a variable to hold the + last found value while(<>) # read the file line-by-line { if (line with timescale info found) store info to the variable lastFoundTimescale if (line with modulename found) process modulename and lastFoundTimescale }
    HTH, Rata

      Hi Ratazong, Thanks! I understand the pseudocode. But I need help in extracting the last found timescale value which is not commented out. And also to ignore any comments that appears next to the valid timescale value in the same line.

        So, what code have you written and how does it fail for your input data?

        A good place to start might be something like the following. You will have to modify the regular expression to detect comments and you will have to add other cases you want to detect.

        #!perl -w use strict; while (<DATA>) { s!\s+$!!; if( m!\btimescale\b! ) { print "Found timescale in [$_]\n"; } else { print "Ignored line [$_]\n"; }; }; __DATA__ =============================================== // 1st commented line //second commented line // timescale 1ns /5 ns timescale 1ns/4ns //this comment should also be ignored module modulename1 (a b c ) some lines.... some lines ... //third commented line //fourth commented line timescale 3ns/5ns module modulename2 (p q r) some lines ... //fifth comment //timescale 4ns/5ns timescale 1ns/3ns module modulename3 (m n p ) some line ... ================================================

        For dealing with the comments, I would just wipe out the comment part of each line immediately after reading it.
        You could do this either using the s/// operator (see the section Regexp Quote-Like Operators in perlop), or using index and substr. The former would be the more "perlish" solution. The latter might look more familiar to you if you come from a language such as C.

        -- 
        Ronald Fischer <ynnor@mm.st>
Re: Extracting pattern from a file (Verilog)
by toolic (Chancellor) on Apr 17, 2012 at 13:38 UTC
    Since your code looks almost like Verilog, have you considered using a Verilog parser? Verilog-Perl and vppreproc in particular may help you weed out comments.

    Also, the Verilog $printtimescale system task can be used inside Verilog source code to show the timescale for each module.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965436]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others rifling through the Monastery: (4)
As of 2014-04-19 21:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    April first is:







    Results (483 votes), past polls