[Omaha.pm] Using map to grab multiple fields from file...

Dan Linder dan at linder.org
Fri Mar 13 10:01:20 PDT 2009


(Sorry for the short e-mail.  Let's try that again... - Dan)

I have a file that has a number of lines in it.  Two lines contain data I
want, the rest are unimportant.  These two lines can appear most anywhere
and in any order.

What I'm currently doing is this:
$val1 = 0;
$val2 = 0;
while (<FILEHANDLE>) {
     $val1 = $1 if /^COUNT:\s*(\d*)$/;
     $val2 = $1 if /^SIZE:\s*(\d*)$/;
     last if ($val1 and $val2);
}

The "last if" statement is my attempt to exit sooner rather than working
through the entire file.

I had tried to setup a map function something like this:
($val1, $val2) = map {
     return ($1, $val2) if /^COUNT:\s*(\d*)$/;
     return ($val1, $2) if /^SIZE:\s*(\d*)$/;
     } <FILEHANDLE>;

...but I get a "can't return outside of subroutine.

Am I better off with my while loop?  It passes the "3AM readability test" in
my opinion.

Dan


-- 
"Quis custodiet ipsos custodes?" (Who can watch the watchmen?) -- from the
Satires of Juvenal
"I do not fear computers, I fear the lack of them." -- Isaac Asimov (Author)
** *** ***** ******* *********** *************
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.pm.org/pipermail/omaha-pm/attachments/20090313/aa551071/attachment.html>


More information about the Omaha-pm mailing list