in reply to How could I make this code more optimized

Taking japhy's advice, I also qr'ed the items inside the @Messages array, and used the reference to the hash as suggested by VSarkiss.
open(RPT,">$DIR/$RPT")||die"Can't open $RPT: $!\n"; print RPT "Report Date: $Date\n"; print RPT "$RPT\n"; print RPT "============================\n"; my($key,$msg,$ref); foreach $key(keys %hash){ $ref=$hash{$key}; foreach $msg(@Messages){ if($ref->{error_message}=~$msg and $ref->{date} eq $Date){ print RPT "$ref->{packet_id}\t$ref->{origin}\n", "$ref->{date}\t$ref->{qualifier}\n", "$ref->{time}\t$ref->{terminal_id}\n", "$ref->{device_id}\t\t$ref->{application_id}\n", "$ref->{catalog_code}\t\t$ref->{function_id}\n", "$ref->{status}\t\t$ref->{severity}\n", "$ref->{error_message}\n", "==================================================== +\n"; }## END OF IF STATEMENT }## END OF INNER FOREACH STATEMENT }## END OF OUTER FOREACH STATEMENT close RPT;
Many thanks to all for your help.

TStanley
--------
"Suppose you were an idiot... And suppose you were a
member of Congress... But I repeat myself." -- Mark Twain

Replies are listed 'Best First'.
Re: Re: How could I make this code more optimized
by buckaduck (Chaplain) on Feb 14, 2002 at 02:07 UTC
    To repeat one of my earlier suggestions:

    Why on Earth are you testing $ref->{date} eq $Date inside of the inner foreach loop? If they're not equal, skip the inner foreach loop altogether!

    Admittedly, this was probably more important back when you were using a regex match instead of eq. But it could still make a big difference...

    Update: And while I'm at it, these lines:

    foreach $key (keys %hash) { $ref = $hash{$key};
    Can be replaced with this:
    foreach $ref (values %hash) {

    buckaduck