Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Someone please verify this.

by ksangam (Initiate)
on Jul 10, 2007 at 03:37 UTC ( [id://625753]=perlquestion: print w/replies, xml ) Need Help??

ksangam has asked for the wisdom of the Perl Monks concerning the following question:

Hi , I am very new to Perl, and I have written a small snippet that will read a file and should replace the pattern matched lines in the file and should print the changed file. Somebody, please tell me if my approach is right.
#!/usr/bin/perl $input_file = "dump.vcd"; open (INPUT, "$input_file"); while ($efile = <INPUT>) { for ($i = 0; $i <$#efile; $i++) { $efile[$i] =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$v +ar $1 $2/ } print "$efile"; } close (INPUT);

Replies are listed 'Best First'.
Re: Someone please verify this.
by Zaxo (Archbishop) on Jul 10, 2007 at 03:58 UTC

    Your C-style for loop runs from zero to minus one, because $#efile refers to the last index of the array @efile, which is created on the spot - empty. Your task will be much simpler if it is written using the idiomatic $_ variable.

    #!/usr/bin/perl use warnings; use strict; my $input_file = 'dump.vcd'; open (INPUT, '<', $input_file) or die $!; while (<INPUT>) { s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2/; print; } close INPUT;
    That has the benefit that the file is read and processed one line at a time, with no space taken for an array to hold it. I added warnings and strict, which would have told you what was wrong. I also improved open to the 3-arg form and added error checking in case open fails.

    I don't believe your character class is exactly what you think it is because of all the commas and escapes. I didn't modify the s/// statement because I didn't know its intent.

    After Compline,
    Zaxo

Re: Someone please verify this.
by GrandFather (Saint) on Jul 10, 2007 at 04:35 UTC

    There are a bunch of errors and dodgy practices there. The first thing that you need to do is add strictures:

    use strict; use warnings;

    Then you need to declare all the variables you use by prefixing them with my. For example:

    my $input_file = "dump.vcd";

    You should always use the three parameter version of open to improve code readability and reduce possible errors of various sorts:

    open INPUT, '<', "$input_file";

    The while loop is reading one line at a time so you don't need a for loop (see perlsyn - foreach) to iterate over anything - $efile is not an array.

    In your regex you use a character set ([!,\#,\",\$,\%,&]), but it isn't what you possibly expect. The characters included in the set are !,#"$%& which includes comma, that may surprise you. Note that none of the quoted characters in the set need to be quoted.

    The complete code rewritten and modified to take itself (on my system) as the input looks like:

    #!/usr/bin/perl use strict; use warnings; my $input_file = "noname1.pl"; open INPUT, '<', "$input_file"; while (my $efile = <INPUT>) { $efile =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2 +/; print $efile; } close INPUT; __DATA__ $var1 1 , x $end $varx 2 ! _ $end $vary 3 # aa $end

    prints:

    #!/usr/bin/perl use strict; use warnings; my $input_file = "noname1.pl"; open INPUT, '<', "$input_file"; while (my $efile = <INPUT>) { $efile =~ s/\$var\w \d+ ([!,\#,\",\$,\%,&]) (\w) \$end/\$var $1 $2 +/; print $efile; } close INPUT; __DATA__ $var , x $var ! _ $vary 3 # aa $end

    Note that I added the __DATA__ (see perldata - Special Literals) section so I could include something for the regex to match.


    DWIM is Perl's answer to Gödel
Re: Someone please verify this.
by Hercynium (Hermit) on Jul 10, 2007 at 03:54 UTC
    A few things off the top of my head...

    $efile isn't an array, so getting it's length in the for-loop wouldn't work. (nor should $efile[$i])

    $var and $end in the regex - where do those come from? They're not declared anywhere...

    Big tip here - changed my life forever when I first heard it: start every script with these two lines:
    use strict; use warnings;
    Your perl skills will improve immensely, as will your code logic, and your teeth and hair will gain a healthy sheen.

      The $ for $var and for $end is quoted so they are literal matches.


      DWIM is Perl's answer to Gödel
Re: Someone please verify this.
by rob_au (Abbot) on Jul 10, 2007 at 04:25 UTC
    You may also want to look at the -i switch to the perl interpreter which allows for in-place editing of files - For example:
    perl -pi -e 's/foo/bar/g' file.txt
    Further details are available in the perlrun man page.

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001000000000'"

Re: Someone please verify this. (Verilog::VCD)
by toolic (Bishop) on Jul 10, 2007 at 13:52 UTC
    If I am not mistaken, it looks like you are parsing a Verilog Value Change Dump (VCD) file. If you have access to Synopsys' VCS simulator -- and that is a big if -- then you should also have acces to the vcat utility.

    vcat is very handy in converting a VCD file into a much simpler format for further parsing.

    The other simulator vendors may have similar capabilities. It might be worth a look if you have some heavy-duty parsing, just so you avoid re-inventing the wheel, as I have done far too often with VCD:)

    Update (12 jan 2012): Verilog::VCD

      I think the Verilog::VCD module is just a VCD parser. Is there any module out there that can make creating VCDs easier? Thanks.
        You are right... Verilog::VCD can only be used to parse VCD files. It can not create a VCD file. I am not aware of any CPAN module which can create a VCD file. This looks like an opportunity for you to contribute.

        Why do you need Perl code to create a VCD file? All Verilog simulators can create VCD files.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-12-07 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (50 votes). Check out past polls.