On 1/27/06, Jeff Pang <[EMAIL PROTECTED]> wrote: > Hello,Chas, > > Thanks advanced for your good suggestions. > I tidy up all the words said by you,and write the code as following.Is it > right? > > while(<$sock>) > { > my ($key,$value) = split; > my $timestamp = time(); > push @records, { time => $timestamp, > key => $key, > amount=> $value, > }; > > shift @records while $records[0]{time} < time() - 5*60; > > my %sum; > for my $rec (@records) { > $sum{$rec->{key}} += $rec->{amount}; > } > > for (keys %sum) > { > do_something() if $sum{$_} > LIMIT; > } > } snip
That looks about right. I would have read all available records (see perldoc -f select) from the socket adding them to @records and then executed the rest of the code, but this way should be fine if do_something() is not overly long. You might want to change shift @records while $records[0]{time} < time() - 5*60; to shift @records while $records[0]{time} < $timestamp - 5*60; -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] <http://learn.perl.org/> <http://learn.perl.org/first-response>