Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

accumulating rex-exp matches in an array

by waxmop (Beadle)
on Jun 25, 2002 at 18:05 UTC ( #177158=perlquestion: print w/ replies, xml ) Need Help??
waxmop has asked for the wisdom of the Perl Monks concerning the following question:

I need to load in data from a file that looks sort of like this:
AAA.1, AAA.2, AAA.3, AAA.4, AAA.5
I want to grab every AAA.number and push it into an array like so:
my @list; while (<INFILE>) { m/(AAA.\d)/; push @list, $1; }
But I don't know how to deal with the fact that there could be 1 or 2 AAA codes on each line. Is there a way to write a regular expression that will match a pattern and return a $1 variable over and over again?

Comment on accumulating rex-exp matches in an array
Select or Download Code
Re: accumulating rex-exp matches in an array
by Sifmole (Chaplain) on Jun 25, 2002 at 18:17 UTC
    while (<INFILE>) { while (s/(AAA\.\d)/xxxxx/) { push (@list, $1); } }
    or if the items are always seperated by a space.
    while (<DATA>) { push (@foo, grep /AAA\.\d/, split(/\s*,\s*/)); }
Re: accumulating rex-exp matches in an array
by joshua (Pilgrim) on Jun 25, 2002 at 18:22 UTC
    my @list; foreach my $file (<INFILE>) { foreach (split /,/, $file) { m/(AAA\.\d)/; push @list, $1; } }
    Added a `\' in front of the `.' in your regex.
Re: accumulating rex-exp matches in an array
by arturo (Vicar) on Jun 25, 2002 at 18:23 UTC
    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

Re: accumulating rex-exp matches in an array
by Anonymous Monk on Jun 25, 2002 at 18:23 UTC
    This is untested but should work, especially if each AAA code is seperated by a comma.
    my @list; while (<INFILE>){ my @line = split/,/,$_; for (@line){ m/(AAA.\d)/; push @list, $1; } }
Re: accumulating rex-exp matches in an array
by thelenm (Vicar) on Jun 25, 2002 at 18:25 UTC
    You can use the array returned by a global match, like so:
    my @list; while (<INFILE>) { push @list, m/(AAA.\d)/g; }

    -- Mike

    --
    just,my${.02}

Re: accumulating rex-exp matches in an array
by japhy (Canon) on Jun 25, 2002 at 18:27 UTC
    You should never use a regex variable without making sure the regex matched. If a line DIDN'T contain "AAA.n", $1 would retain its value!

    Anyway, you want to use the /g modifier:

    while (<INFILE>) { push @list, /AAA.\d/g; # parens not needed in regex here }

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker, who'd like a job (NYC-area)
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://177158]
Approved by particle
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (7)
As of 2014-12-26 01:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (163 votes), past polls