Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Re: Regex shows only last match multiple times?

by Eily (Parson)
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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1074790]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (3)
As of 2018-01-20 23:29 GMT
Find Nodes?
    Voting Booth?
    How did you see in the new year?

    Results (227 votes). Check out past polls.