Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Regex shows only last match multiple times?

by Eily (Vicar)
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 chilling in the Monastery: (3)
As of 2017-04-30 05:27 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (534 votes). Check out past polls.