Re: Storing RegExp matches in an array
- From: "Paul Lalli" <mritty@xxxxxxxxx>
- Date: 25 Jan 2007 09:53:27 -0800
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
.
- References:
- Storing RegExp matches in an array
- From: Idgarad
- Storing RegExp matches in an array
- Prev by Date: Storing RegExp matches in an array
- Next by Date: Calling functions from modules
- Previous by thread: Storing RegExp matches in an array
- Next by thread: Calling functions from modules
- Index(es):
Relevant Pages
|