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

Re: Regex shows only last match multiple times?

by Eily (Chaplain)
on Feb 13, 2014 at 10:43 UTC ( #1074790=note: print w/ replies, xml ) Need Help??


in reply to Regex shows only last match multiple times?

A data sample would have been helpful. Not your whole data set, but just enough to try your code by ourselves.

The problem with your code is that, foreach ( $w =~ /(capture)/g) will run the regex through the whole string, setting each time $1, and then start looping through the list. But the elements will be aliased to $_, not $1 which won't change. Write print "Photo: $count $_\n" instead, and all shall be well.

Instead of opening $ARGV[0] yourself, you could try using the diamond operator's magic.

my $count = 0; while(<>) { $count+= () = m< photo/([a-z]+\.[a-z]{3}) >gxi; }
This will loop through all the files in the parameters (which allow you to supply several filenames instead of one. The currently processed file is $ARGV. If @ARGV is empty, your script will work on STDIN instead.


Comment on Re: Regex shows only last match multiple times?
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-10-31 06:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (215 votes), past polls