Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Issue with regex matching

by wanna_code_perl (Pilgrim)
on Aug 28, 2013 at 23:16 UTC ( #1051329=note: print w/ replies, xml ) Need Help??


in reply to Issue with regex matching

You say you want to count how many times the variable appears in the file, but your code tries to count the number of lines that contain the variable. If you did intend the former, you can count the number of occurrences in one go:

while (<FILE>) { $count += () = /\Q$constant\E/g; } print "$constant occurs $count times\n";

In my experience, small files (anything less than several megabytes) often perform better if you slurp the whole thing in, rather than looping over each line:

use File::Slurp; my $constant = '$_'; $_ = read_file('filename.pl'); $count = () = /\Q$constant\E/g; print "$constant occurs $count times\n";


Comment on Re: Issue with regex matching
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2014-09-20 20:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (161 votes), past polls