473,327 Members | 2,112 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

Delete duplicate keys in hash from file

I managed to write a file to a hash. However the file contains multiple keys and value in the format listed. Note how the values are sorted in descending order.

i.e. of file written to hash

Expand|Select|Wrap|Line Numbers
  1. ASDF 5.908
  2. ASDF 5.902
  3. ASDF 5.123
  4. QWER 3.098
  5. QWER 3.023
  6. QWER 3.013
  7. ZXCV 2.345
  8. ZXCV 2.312
  9. ZXCV 2.123
  10.  
I would like to remove the duplicate keys with the lower values I can write a new file with only the highest values for each key

i.e.
Expand|Select|Wrap|Line Numbers
  1. ASDF 5.908
  2. QWER 3.098
  3. ZXCV 2.345
  4.  
I tried making a second hash:

Expand|Select|Wrap|Line Numbers
  1. %hash2;
  2. for $key (keys %hash2) {
  3.     $value_key = "@{[keys $key}]}";
  4.     if (exists $value_key) {
  5.         delete $key;
  6.     }
  7. }
  8. undef hash2;
  9. print %hash2;
  10. exit;
  11.  
Jun 18 '07 #1
1 7765
miller
1,089 Expert 1GB
Expand|Select|Wrap|Line Numbers
  1. my %hash = ();
  2. while (<DATA>) {
  3.     chomp;
  4.     my ($key, $val) = split ' ', $_, 2;
  5.     if (! exists $hash{$key} || $hash{$key} < $val) {
  6.         $hash{$key} = $val;
  7.     }
  8. }
  9.  
  10. # Open Output to new file
  11. foreach my $key (sort {$a cmp $b} keys %hash) {
  12.     print "$key $hash{$key}\n";
  13. }
  14.  
  15. __DATA__
  16. ASDF 5.908
  17. ASDF 5.902
  18. ASDF 5.123
  19. QWER 3.098
  20. QWER 3.023
  21. QWER 3.013
  22. ZXCV 2.345
  23. ZXCV 2.312
  24. ZXCV 2.123
  25.  
- Miller
Jun 18 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

8
by: Jan-Erik Meyer-Lütgens | last post by:
In the Python Language Reference, I found the following statements about using objects as dictionary keys: 1. "__hash__() should return a 32-bit integer." 2. "The only required property is...
57
by: Egor Bolonev | last post by:
why functions created with lambda forms cannot contain statements? how to get unnamed function with statements?
7
by: Lowell Kirsh | last post by:
I have a script which I use to find all duplicates of files within a given directory and all its subdirectories. It seems like it's longer than it needs to be but I can't figure out how to shorten...
44
by: Xah Lee | last post by:
here's a large exercise that uses what we built before. suppose you have tens of thousands of files in various directories. Some of these files are identical, but you don't know which ones are...
11
by: google_groups3 | last post by:
Hi all. I currently have 2 text files which contain lists of file names. These text files are updated by my code. What I want to do is be able to merge these text files discarding the...
90
by: Christoph Zwerschke | last post by:
Ok, the answer is easy: For historical reasons - built-in sets exist only since Python 2.4. Anyway, I was thinking about whether it would be possible and desirable to change the old behavior in...
5
by: andrewcw | last post by:
I have a VB 5 module that duplicates the FCIV.exe from Microsoft. I need to move an application forward to C#, but the samples for MD5 hash using the framework I tried gave different hashes. ...
24
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to...
5
by: nirmal1349 | last post by:
Hi, I have a hash in hash file in perl which looks some thing like this: $FIELDS = { 'abc' => { 'Description' => { 'Purpose' => 'some data is present',...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.