The UNIX and Linux Forums  
Hello and Welcome from United States to the UNIX and Linux Forums! Thank You for Visiting and Joining Our Global Community.

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
.
google unix.com



Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Perl Hash Harikrishna Shell Programming and Scripting 1 06-04-2008 07:03 AM
Perl Hash Harikrishna Shell Programming and Scripting 1 06-02-2008 11:45 PM
Hash in perl Harikrishna Shell Programming and Scripting 1 06-02-2008 04:00 AM
perl array question from going through hash hankooknara Shell Programming and Scripting 2 07-29-2007 09:53 PM
perl hash question hankooknara Shell Programming and Scripting 3 07-28-2007 01:31 PM

Closed Thread
English Japanese Spanish French German Portuguese Italian Dutch Swedish Russian Norwegian Hungarian Hebrew Danish Powered by Powered by Google
 
LinkBack Thread Tools Search this Thread Rate Thread Display Modes
  #1 (permalink)  
Old 12-20-2001
deadletter deadletter is offline
Registered User
  
 

Join Date: Dec 2001
Location: Houston, TX
Posts: 4
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 (permalink)  
Old 12-28-2001
auswipe's Avatar
auswipe auswipe is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Wide Awake Wylie, Texas
Posts: 535
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 (permalink)  
Old 12-28-2001
deadletter deadletter is offline
Registered User
  
 

Join Date: Dec 2001
Location: Houston, TX
Posts: 4
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 (permalink)  
Old 12-28-2001
auswipe's Avatar
auswipe auswipe is offline Forum Advisor  
Registered User
  
 

Join Date: Nov 2001
Location: Wide Awake Wylie, Texas
Posts: 535
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 (permalink)  
Old 07-17-2008
summer_cherry summer_cherry is offline Forum Advisor  
Registered User
  
 

Join Date: Jun 2007
Location: Beijing China
Posts: 1,078
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 (permalink)  
Old 07-17-2008
matrixmadhan matrixmadhan is offline Forum Advisor  
Technorati Master
  
 

Join Date: Mar 2005
Location: leaf node in B+ tree
Posts: 2,944
Quote:
Originally Posted by auswipe View Post
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
  #7 (permalink)  
Old 07-17-2008
KevinADC KevinADC is offline Forum Advisor  
Registered User
  
 

Join Date: Jan 2008
Posts: 731
Quote:
Originally Posted by summer_cherry View Post
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.
Sponsored Links
Closed Thread

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT -4. The time now is 07:05 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited. Language Translations Powered by .
vBCredits v1.4 Copyright ©2007 - 2008, PixelFX Studios
The UNIX and Linux Forums Content Copyright ©1993-2009. All Rights Reserved.Ad Management by RedTyger

Content Relevant URLs by vBSEO 3.2.0