Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Regex shows only last match multiple times?

by zeltus (Sexton)
on Feb 13, 2014 at 10:17 UTC ( #1074778=note: print w/ replies, xml ) Need Help??


in reply to Regex shows only last match multiple times?

You can make your code rather more simpler and hence, easier to read:

Initialise $count to zero

my $count = 0;

...or, even more simply

my $count;

Then you don't need that perverse

$count = $count - 1;

statement. And even if you did want it, what's wrong with

$code--;

(always bearing in mind the old TIMTOWTDI acronym of course!)

The next simplification, if the file really does consist of just one line, is to slurp it in, in one hit

@array = <$fh>; # Reads all lines into array $singleLine = <$fh>; # reads one line only

Finally, and this is where I think your question might get answered, you can capture regex hits to an array and then simply count the array...

my @count = $w =~ /$x/g; my $total = @count;

With no loops making it look more complicated than it is, you can then start to pin down what is going on here. Perhaps use some test data/test regex to give a simpler starting block. I would also be suspicious of perl being greedy in that there regex. But first, create some test data and make sure you can "prove" your code before hitting the real data.


Comment on Re: Regex shows only last match multiple times?
Select or Download Code
Re^2: Regex shows only last match multiple times?
by GertMT (Friar) on Feb 13, 2014 at 10:47 UTC
    thanks,
    changed a few more things indeed and it now works as expected

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (9)
As of 2014-08-01 10:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (0 votes), past polls