Hash Question in Perl

Learning Perl here, so bear with me... Have a hash that i need to delete the entry out of and am having problems doing that. Basically, I need to delete all entries from the hash that have values over 5,000,000. What I am trying to do is to find each entry and delete it. Does not work - I have made many changes but either the program will not compile or the hash entries are still there
Can gurus help?

#!/usr/bin/perl
%messages = qw (1 342 2 4567 3 5999876 4 5768);
foreach $value (values (%messages)) > 5000000 {
delete $value.... #this is the line
}
print %messages, "\n";

While I am not a guru, here is a quick and easy solution that I came up with...

#!/usr/bin/perl

%messages = qw (1 342 2 4567 3 5999876 4 5768);

foreach $value (sort { $messages{$b} <=> $messages{$a} } (keys(%messages))) {
  if ($messages{$value} > 5000000) {
    delete $messages{$value}; # .... #this is the line
  };
}

foreach $value (keys(%messages)) {
  print "$value - $messages{$value}\n";
};

I am sure that there is a better way to do it. AFAIK, there isn't a break available for a foreach loop to stop the looping when a $messages{$value} less than 5,000,000 is found.

That is a clever piece of code, man. Thanks a lot, man. BTW, I posted the same question on techrepublic.com and could not get any decent answers. That makes you a guru.

I wonder when can I expect my GuruMan patch in the mail?

Ha!Ha!Ha!Ha!

I make myself laugh.

example:
delete those <=5

%hash=(a,1,b,2,c,3,d,5,e,9,f,10,u,15);
foreach $key (keys %hash){
delete($hash{$key}) if $hash{$key}<=5;
}
foreach $key (keys %hash){
print $key,"-->",$hash{$key},"\n";
}

Just to kindle some good suggestion -
Does sorting here makes it a better solution, unless its an array and we are sure of the index, sorting a hash will not help us to escape from scannig each and every element of the hash.

so, basically I mean - even if you are sorting, you have to scan through each and every element of the hash.

I feel, sorting is not needed here
:slight_smile:

This is the third or forth ancient thread you have dug up to post a reply to, see the date of the threads before replying:

12-28-2001 <-----------

Even a thread one/two months old is pushing it, but 7+ years is beyond ridiculous.