Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re: Someone please verify this.

by GrandFather (Saint)
on Jul 10, 2007 at 04:35 UTC ( [id://625761]=note: print w/replies, xml ) Need Help??


in reply to Someone please verify this.

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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (9)
As of 2024-03-28 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found