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

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:  <%-(-(-(-<

Replies are listed 'Best First'.
Re^7: Remove similar key=value pair from HOH
by vaibhav07 (Acolyte) on Apr 12, 2015 at 18:48 UTC

    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:  <%-(-(-(-<

Re^7: Remove similar key=value pair from HOH
by Laurent_R (Canon) on Apr 12, 2015 at 22:10 UTC
    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!