I am trying to remove duplicates from a HoH. I've almost got it, I think, but I'm missing some key element. Any chance you can spot my mistake? I've been at it two days.
The data is moved into a HoH using the HoH example in the "Programming Perl, 3rd Ed". I've included the some of the offending data (pulled from an array in memory) and listed the import hash function as well as the duplicate removal section of code.
I'm trying to remove duplicate partnum's from the data and combine the tags.
#!/usr/bin/perl # #use strict; use warnings; use File::Find; use List::Compare; use Data::Dumper; use feature "switch"; ## @tmp2 is the data given in the code snippet below my $who=(); my $field=(); foreach (@tmp2) { s/^(.*?):\s*//; $who=$1; $rec = {}; $HoH{$who}=$rec; for $field (split(/,/)) { ($key,$value) = split /=/, $field; $rec->{$key} =$value; } } ## Testing to see if the hash is working. ## Uncomment Section below to test. my $iter=(); my $iter2=(); for $iter (keys %HoH) { print "$iter: "; for $iter2 ( keys %{ $HoH{$iter}} ) { print "$iter2=$HoH{$iter}{$iter2} ,"; } print "\n"; } ############################################## ## ## Remove duplicate part numbers and combine the tags, if necessary. ## ## print "######################################"; print "Duplicate Removal Testing\n"; my %tmpHoH=%HoH; my $keyouter; my $valueouter; my $keydup; my $valuedup; my @keydelete=(); while ( ($keyouter,$valueouter)= each %HoH){ while ( ($keydup,$valuedup) = each %tmpHoH){ if($keyouter eq $keydup){ next; } if($HoH{$keyouter}{partnum} eq $tmpHoH{$keydup}{partnum}){ print "**********\n"; print "key: $keyouter, value: $HoH{$keyouter}{partnum}, tag= $HoH{ +$keyouter}{tags}\n"; print "key: $keydup, value: $tmpHoH{$keydup}{partnum}, tag= $tmpHo +H{$keydup}{tags}\n"; $HoH{$keyouter}{tags}= $HoH{$keyouter}{tags} ." ". $tmpHoH{$keydup +}{tags}; print "$tmpHoH{$keyouter}{tags}\n"; print "**********\n"; push(@keydelete,"$keyouter=>$keydup"); } } } print @keydelete;
The Data:
413: partnum=2204133000,description=PRESS GAUGE,quantity=1.0000,tags=P +I-412 414: partnum=2202261000,description=THERMOWELL,quantity=2.0000,tags= 415: partnum=2201176000,description=THERMOMETER,quantity=2.0000,tags= 581: partnum=2204227002,description=TEMP TRANSMITTER,quantity=1.0000,t +ags=TE/TT-102 582: partnum=2201176000,description=THERMOMETER,quantity=3.0000,tags=T +I-100 TI-101 TI-200 576: partnum=2204133000,description=PRESS GAUGE,quantity=1.0000,tags=P +I-400
Any help would be highly appreciated! Thank you, Shawn Way
In reply to Removing Duplicates in a HoH by DunLidjun
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |