raptor has asked for the wisdom of the Perl Monks concerning the following question:

hi,
what is the easier way to remove repeated info from structure like this :
%struct = { row1 => { key0 => 'val0', key1 => 'val1', keyX => 'valX' }, row2 => { key0 => 'val0', key1 => 'val1', keyX => 'valX' }, row3 => { key0 => 'val0', key1 => 'val1', keyX => 'valX' }, .... rowX => { key0 => 'val0', key1 => 'val1', keyX => 'valX' }, };

if all values from 'row1' match all values from say 'row3'and 'row5' (after stripping the white space)... Then only 'row1' and all other non repating rows should stay...
any ideas to do this in the easier, shorter, faster way..:")
Thanx....

Replies are listed 'Best First'.
Re: removing the repeated info...
by jlongino (Parson) on Aug 04, 2001 at 01:28 UTC
    raptor,

    Note that I changed your outer %struct squigglies to parens. I've left some debug prints in the following code for illustrative purposes:

    #!/usr/local/bin/perl use strict; my %struct = ( row1 => { key0 => 'val0', key1 => 'val2', keyX => 'valX' }, row2 => { key0 => 'val0', key1 => 'val1', keyX => 'valX' }, row3 => { key0 => 'val0', key1 => 'val1', keyX => 'valY' }, row9 => { key0 => 'val0', key1 => 'val1', keyX => 'valY' }, rowX => { key0 => 'val0', key1 => 'val1', keyX => 'valX' } ); my %ndxhash = (); print "Before deletions:\n"; foreach my $row (keys(%struct)) { print "\$row: $row"; print " key0: $struct{$row}{key0} "; print " key1: $struct{$row}{key1} "; print " keyX: $struct{$row}{keyX} \n"; ## build a key to determine a uniqueness in %ndxhash my $ndxkey = Trim($struct{$row}{key0}) . Trim($struct{$row}{key1}) . Trim($struct{$row}{keyX}); if (exists $ndxhash{$ndxkey}) { ## this one is a duplicate delete $struct{$row}; } else { ## this is a unique one, add it to the %ndxhash $ndxhash{$ndxkey} = 1; } } print "\n\nAfter deletions\n\n"; foreach my $row (keys(%struct)) { print "\$row: $row"; print " key0: $struct{$row}{key0} "; print " key1: $struct{$row}{key1} "; print " keyX: $struct{$row}{keyX} \n"; } sub Trim { my $InVar = shift; for ($InVar) { s/^\s+//; s/\s+$//; } if (! $InVar) { $InVar = " "; } return $InVar; }
    Execution Results
    Before Deletions: $row: rowX key0: val0 key1: val1 keyX: valX $row: row9 key0: val0 key1: val1 keyX: valY $row: row1 key0: val0 key1: val2 keyX: valX $row: row2 key0: val0 key1: val1 keyX: valX $row: row3 key0: val0 key1: val1 keyX: valY After Deletions $row: rowX key0: val0 key1: val1 keyX: valX $row: row9 key0: val0 key1: val1 keyX: valY $row: row1 key0: val0 key1: val2 keyX: valX
    Hope this is clear enough.

    Update: removed unused variable in Trim sub

      Note that I changed your outer %struct squigglies to parens
      ]- yep... I'm making this mistake very often :")

      Thanx alot ....