Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Regex shows only last match multiple times?

by Eily (Curate)
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 contemplating the Monastery: (13)
As of 2015-07-02 21:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (45 votes), past polls