Kent, Mr. John (Contractor) wrote:
> Greetings,
>
> Trying to be "cool" and apply map to the following lines of code
> (which work fine as shown)
>
> @TIMES is an array of lines that look like
> Time: Thu Feb 3 15:10:39.290 GMT 2005 End: Default.2.def-edtp0.on
> Time: user 0.29 sec Delta: Default.2.def-edtp0.on
> Time: Thu Feb 3 15:10:43 GMT 2005 Start:
> Default.1.def-slpr-2m-t-10m-wind0.on
> Time: Thu Feb 3 15:10:43.580 GMT 2005 End:
> Default.1.def-slpr-2m-t-10m-wind0.on
> Time: user 0.58 sec Delta: Default.1.def-slpr-2m-t-10m-wind0.on
>
> use Tie::IxHash;
> tie %BY_PROJ_HASH, "Tie::IxHash";
>
> my @DELTAS = grep (/Delta/, @TIMES);
>
> foreach (@DELTAS) {
> $_ =
> /(\d+\.\d+)\s+sec\s+Delta:\s+[Default\.]*\s*(.+)[\.on]*$/; my
> $proj = $2; my $time = $1;
If the regex doesn't match, $1 and $2 will be unchanged. You should not use
them without checking for the match.
I prefer a list assignment like:
my ($proj, $time) =
/(\d+\.\d+)\s+sec\s+Delta:\s+[Default\.]*\s*(.+)[\.on]*$/;
This will set the vars to undef if there is no match.
>
> print "<br>$proj => $time\n" if ($DEBUG == 1);
> # Push the value into the hash making the value for the hash
> # an array (See Perl Cookbook Recipe 5.7)
> push( @{$BY_PROJ_HASH{$proj}},$time);
>
> }
>
> So far haven't had any luck using map to create the hash of arrays
> %BY_PROJ_HASH.
I don't think a map() construct would improve on what you're doing, since
$proj presumably can vary with each iteration. Leave it like it is.
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>