in reply to Re^3: Remove similar key=value pair from HOH
in thread Remove similar key=value pair from HOH

bash-4.2$ cat dump use Data::Dumper; my %hash =( 'script' => 'foo.pm', 'params' => { 'err' => '99', 'FILE' => 'fileA' }, 'par_global' => { 'err' => '99', 'FILE_READ' => 'fileB', }, 'testset' => ['test1'] ); print Dumper( \%hash ) . "\n";
bash-4.2$ perl dump $VAR1 = { 'params' => { 'FILE' => 'fileA', 'err' => '99' }, 'script' => 'foo.pm', 'par_global' => { 'FILE_READ' => 'fileB', 'err' => '99' }, 'testset' => [ 'test1' ] };

My question is while traversing the hash if'err' => '99' is mentioned again, it should just avoid it.

Replies are listed 'Best First'.
Re^5: Remove similar key=value pair from HOH
by pme (Monsignor) on Apr 12, 2015 at 07:25 UTC
Re^5: Remove similar key=value pair from HOH
by AnomalousMonk (Archbishop) on Apr 12, 2015 at 07:24 UTC
    ... if'err' => '99' is mentioned again ... just avoid it.

    Do you mean "If the key 'err' is mentioned again with the specific value of '99', avoid it", or "If the key 'err' is mentioned again with any value, avoid it"?


    Give a man a fish:  <%-(-(-(-<

Re^5: Remove similar key=value pair from HOH
by Laurent_R (Canon) on Apr 12, 2015 at 09:47 UTC
    My question is while traversing the hash if'err' => '99' is mentioned again, it should just avoid it.
    No, because they are in different "sub-hashes". The first "err" key-value pair is in the hash referenced by 'params', while the second one is in the hash referenced by 'par_global'. Two different hashes.

    Je suis Charlie.
      No, because they are in different "sub-hashes".

      But my guess (and at this point that's all it is) about what vaibhav07 wants is some kind of custom traversal routine that will avoid some or all keys that have already been "seen", even if they were members of separate and distinct (sub-)hashes. Or something like that. Maybe. Update: As pme has suggested, a strong scent of XY Problem here.


      Give a man a fish:  <%-(-(-(-<

        Thank perl monks for your time and input. I have myself written a logic for it.

        my @arr; foreach my $key ( keys %hash ) { if ( ref $hash{$key} ne 'HASH' && ref $hash{$key} ne 'ARRAY' ) { if ( ( defined $key ) && ( $key eq 'script' ) ) { push( @arr, $hash{$key} ); } } if ( ref $hash{$key} eq 'HASH' ) { foreach my $k ( keys %{ $hash{$key} } ) { my $str = $k . "=" . $hash{$key}{$k}; push( @arr, $str ); } } } print Dumper( \@arr ); my %freq = (); my @unique = grep { !$freq{$_}++ } @arr; print Dumper( \@unique ); Output : $VAR1 = [ 'FILE=fileA', 'err=99', 'foo.pm', 'FILE_READ=fileB', 'err=99' ]; $VAR1 = [ 'FILE=fileA', 'err=99', 'foo.pm', 'FILE_READ=fileB' ];
        Yes, from vaibhav07's answer, it clearly appears, AnomalousMonk, that you were right. My only point was to explain why the OP's code did not work as expected, I did not try to make assumptions about the desired result (although I had some ideas thereon).

        Je suis Charlie.