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

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.

Replies are listed 'Best First'.
Re^6: Remove similar key=value pair from HOH
by AnomalousMonk (Archbishop) on Apr 12, 2015 at 16:46 UTC
    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' ];
        my %freq = ();
        my @unique = grep { !$freq{$_}++ } @arr;

        You may be interested in List::MoreUtils::uniq:
            use List::MoreUtils qw(uniq);
            my @unique = uniq @arr;


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

      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.
        > My only point was to explain why the OP's code did not work as expected

        Look what the OP wrote (in a badly formatted way) is

        Output from my code FILE=fileA err=99 script:foo.pm foo.pm FILE_READ=fileB err=99 err=20 testset:test1 Output required FILE=fileA err=99 script:foo.pm foo.pm FILE_READ=fileB err=20 testset:test1

        I have the biggest problems seeing a code who could possibly produce err=99 and err=20 out of a hash.

        He never gave us the full picture, we had no possibility to know what his real problem is and "what didn't work as expected."

        And for completeness the "solution" he posted is IMHO still buggy, b/c it'll filter ALL duplicates, not only for key 'err'.

        Cheers Rolf
        (addicted to the Perl Programming Language and ☆☆☆☆ :)
        Je suis Charlie!