Duly noted. Thanks for the tip on perltidy. I'd never heard of that script before.

What I am looking for is to remove the duplicate part numbers and combine the tags (and add the quantities) similar to below:

413: quantity=2.0000 ,description=PRESS GAUGE ,tags=PI-412 PI-400 ,par +tnum=2204133000 , 414: quantity=2.0000 ,description=THERMOWELL ,tags= ,partnum=220226100 +0 , 415: quantity=5.0000 ,description=THERMOMETER ,tags= TI-100 TI-101 TI- +200 ,partnum=2201176000 ,

The original information is below:

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= +, 576: partnum=2204133000,description=PRESS GAUGE,quantity=1.0000,tags=P +I-400 , 582: partnum=2201176000,description=THERMOMETER,quantity=3.0000,tags=T +I-100 TI-101 TI-200

The code is below:

#!/usr/bin/perl # use strict; use warnings; my @tmp2 = ( "413: partnum=2204133000,description=PRESS GAUGE,quantity=1.0000,tags= +PI-412", "414: partnum=2202261000,description=THERMOWELL,quantity=2.0000,ta +gs=", "415: partnum=2201176000,description=THERMOMETER,quantity=2.0000,t +ags=", "576: partnum=2204133000,description=PRESS GAUGE,quantity=1.0000,tags= +PI-400", "582: partnum=2201176000,description=THERMOMETER,quantity=3.0000,tags= +TI-100 TI-101 TI-200" ); # # Move the data into a hash %HoH # my $who = (); my $field = (); my %HoH = (); my $rec = (); my $key = (); my $value = (); foreach (@tmp2) { s/^(.*?):\s*//; $who = $1; $rec = {}; $HoH{$who} = $rec; for $field ( split(/,/) ) { ( $key, $value ) = split /=/, $field; $rec->{$key} = $value; } } print "\nHash complete\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= $tmpHoH{$keydup} +{tags}\n"; $HoH{$keyouter}{tags} = $HoH{$keyouter}{tags} . " " . $tmpHoH{$keydup}{tags}; print "$tmpHoH{$keyouter}{tags}\n"; print "**********\n"; push( @keydelete, "$keyouter=>$keydup" ); } } } print "######################################\n"; ############################################## ############################################# # ## 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"; }

In reply to Re^2: Removing Duplicates in a HoH by DunLidjun
in thread Removing Duplicates in a HoH by DunLidjun

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.