Hash Question in Perl


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Hash Question in Perl
# 1  
Old 12-20-2001
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";
# 2  
Old 12-28-2001
Quote:
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?
While I am not a guru, here is a quick and easy solution that I came up with...

Code:
#!/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.
# 3  
Old 12-28-2001
Damn, you are good

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.
# 4  
Old 12-28-2001
Quote:
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.
# 5  
Old 07-17-2008
example:
delete those <=5

Code:
%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";
}

# 6  
Old 07-17-2008
Quote:
Originally Posted by auswipe
While I am not a guru, here is a quick and easy solution that I came up with...

Code:
#!/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.
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
Smilie
# 7  
Old 07-17-2008
Quote:
Originally Posted by summer_cherry
example:
delete those <=5

Code:
%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";
}

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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Programming

Perl: restrict perl from automaticaly creating a hash branches on check

My issue is that the perl script (as I have done it so far) created empty branches when I try to check some branches on existence. I am using multydimentional hashes: found it as the best way for information that I need to handle. Saing multidimentional I means hash of hashes ... So, I have ... (2 Replies)
Discussion started by: alex_5161
2 Replies

2. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

3. Shell Programming and Scripting

perl hash - using a range as a hash key.

Hi, In Perl, is it possible to use a range of numbers with '..' as a key in a hash? Something in like: %hash = ( '768..1536' => '1G', '1537..2560' => '2G' ); That is, the range operation is evaluated, and all members of the range are... (3 Replies)
Discussion started by: dsw
3 Replies

4. Shell Programming and Scripting

Perl Hash:Can not keep hash data in the same order that it was inserted

Can Someone explain me why even using Tie::IxHash I can not get the output data in the same order that it was inserted? See code below. #!/usr/bin/perl use warnings; use Tie::IxHash; use strict; tie (my %programs, "Tie::IxHash"); while (my $line = <DATA>) { chomp $line; my(... (1 Reply)
Discussion started by: jgfcoimbra
1 Replies

5. Shell Programming and Scripting

Perl question - print last element in a hash?

I am modifying someone else's code. There is a foreach statement printing the contents of a hash. Can someone give me an example of printing the last element in a hash? The output currently is A B C D E I want the output to be E (1 Reply)
Discussion started by: streetfighter2
1 Replies

6. Shell Programming and Scripting

PERL - another quick hash of hashes question

Hi, sorry, two hash related questions in one day .. but this has got me a bit stuck. I have a mysql database table that kind of looks like this, the table is called "view1" and a snippet of that table (SELECT'ing just rows with serial number 0629AN1200) is below serial nic_name ... (2 Replies)
Discussion started by: hcclnoodles
2 Replies

7. Shell Programming and Scripting

perl hash

This is my data 1 0 1 0 1 1 1 2 1 6 1 7 Assume that first field is key and 2nd field is value I want to create a hash in perl, on this data. My hash should having uniq key and all values by , separated. 1,0,0,1,2,6,7 1 will be my key and rest of are should be values. (3 Replies)
Discussion started by: pritish.sas
3 Replies

8. Shell Programming and Scripting

Hash in perl

Hi Help me with some good links of Hash with in Hash .(Multidimensional hash).. Regards Harikrishna (1 Reply)
Discussion started by: Harikrishna
1 Replies

9. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

10. Shell Programming and Scripting

perl hash question

Below is one article I was reading and I don't understand the $seen{$_} = 1 Can someone explain this in easier terms plesae? ____________________________________________________________ We can also take advantage of the uniqueness property of hash keys to filter out duplicates from... (3 Replies)
Discussion started by: hankooknara
3 Replies
Login or Register to Ask a Question