Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

Re: accumulating rex-exp matches in an array

by arturo (Vicar)
on Jun 25, 2002 at 18:23 UTC ( #177163=note: print w/replies, xml ) Need Help??

in reply to accumulating rex-exp matches in an array

And lo, it was written in the book of perlop, that one would be able to call upon the /g modifier, and do that which ye seek:
my @list; while (<INFILE>) { push @list, m/(AAA\.\d)/g; }

And, yea, moreover it is also not good practice to blindly assign $1 and its brethren without checking whether they are defined first, or using the glories of the list assignment which hath been provided for such uses and others

OK, enough of that ... your code has a problem, in that it will attempt to match and will push the value of $1 onto the array WHETHER OR NOT that match is successful. The terse example above is functionally (in the present context) equivalent to:

while (<INFILE>) { if (my @matches = m/(AAA\.\d)/g ) { push @list, @matches; } }

The match operator, with parens and /g, returns a list of the matches in the line, which get assiged to the @matches array.

Oh, yeah, and you'll notice that since . is a metacharacter in regular expressions, you should escape it if you want to match a literal "." rather than "any character (except newline)".


Log In?

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

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (6)
As of 2021-01-21 19:21 GMT
Find Nodes?
    Voting Booth?