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

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
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)".


In reply to Re: accumulating rex-exp matches in an array by arturo
in thread accumulating rex-exp matches in an array by waxmop

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others contemplating the Monastery: (6)
    As of 2021-01-27 21:12 GMT
    Find Nodes?
      Voting Booth?