473,461 Members | 1,924 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 7775
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',...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...

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.