Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: accumulating rex-exp matches in an array

by arturo (Vicar)
on Jun 25, 2002 at 18:23 UTC ( [id://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)".

HTH

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-04-19 22:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found