Re: Storing RegExp matches in an array



On Jan 25, 12:29 pm, "Idgarad" <idga...@xxxxxxxxx> wrote:
given a file with a variable number of lines I want to grab lines that
match a regexp and store the matches in an array such that the results
look like:

@ARRAY=
(line1match1,line1match2, etc)
(line2match1,line2match2, etc)

I would wager it is like =

@array = $fileToSearch =~ /([\w\d]*) - ([\w\d]*) - ([\w\d]*)/ # to
process the while file

or would I have to loop it for each line and push the results onto the
array?

You don't *have* to, but you should. You usually don't want to store
the entire file's contents in memory. It's wasteful, and is not
expandable to large files.

my @array;
push @array, /([\w\d]*) - ([\w\d]*) - ([\w\d]*)/ while <$fh>;

If you *really* are opposed to that and want to do it all at once, you
could use a map:

my @array = map { /([\w\d]*) - ([\w\d]*) - ([\w\d]*)/ } <$fh>;

But again, that's strongly not recommended.

Paul Lalli

.



Relevant Pages

  • Re: Finding the nearest match without reusing results
    ... comparing an array with a value generates an array of Trues and ... Any diff with wrong State or with used Store is implicitly zeroed out. ... Each must be larger than the absolute value of the maximum Sales ... go.....it just needs to return the closest match. ...
    (microsoft.public.excel.programming)
  • Re: read keyboard input and storing in an array?
    ... > I'm trying to store user input in an array, ... You have the beginnings of that logic already since your prompt tells the ... 201st value since the array only has room for 200 values. ... This will force you to store the int values as Integer ('Integer' ...
    (comp.lang.java.help)
  • Re: Challenge: reading ascii data
    ... to store all the data before producing any output. ... would be bad practice in terms of memory consumption to use a standard ... So I use hashes to create a two-level "sparse array", ... Well the original problem definition was: ...
    (comp.lang.fortran)
  • Re: attempting an actual game...
    ... >>> and inflexible by the absurd decision to use a bit array for square ... as then one has 8 bits in which to store a color and a few flags ... Using a 2D int (or, ... > Change direction and you may eventually complete a game. ...
    (comp.games.development.programming.misc)
  • Re: Sparse arrays
    ... >access array element, set it, sit for a bit, and read it back ... ONLY operations are STORE and RETRIEVE with no structure to the ... If you can tolerate a bit of inefficiency ... They live in sorted lists. ...
    (comp.lang.fortran)