Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Remove similar key=value pair from HOH

by vaibhav07 (Acolyte)
on Apr 11, 2015 at 19:13 UTC ( [id://1123144]=perlquestion: print w/replies, xml ) Need Help??

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

my %hash =( 'script' => 'foo.pm', 'params' => { 'err' => '99', 'FILE' => 'fileA' }, 'par_global' => { 'err' => '99', 'err' => '20', 'FILE_READ' => 'fileB', }, 'testset' => ['test1'] );

I am extracting key value pairs from %hash, but in case if key=values are similar, it should pick only one entry of it rather than picking both the entries.

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

Should list only one entry of err=99 rather than two entries

Code used

foreach my $key (keys %hash) { if ( ref $hash{$key} ne 'HASH' && ref $hash{$key} ne 'ARRAY' ) { print"$key:$hash{$key}\n"; if( (defined $key) && ($key eq 'script') ) { print"$hash{$key}\n"; } } if( ref $hash{$key} eq 'HASH' ) { foreach my $k (keys %{$hash{$key}}) { print"$k=$hash{$key}{$k}\n"; } } elsif( ref $hash{$key} eq 'ARRAY' ) { print"$key:$hash{$key}[0]\n"; } }

Replies are listed 'Best First'.
Re: Remove similar key=value pair from HOH
by LanX (Saint) on Apr 11, 2015 at 19:31 UTC
    Please wrap the output also in <code> tags, it's not readable

    anyway this part

    'par_global' => { 'err' => '99', 'err' => '20', 'FILE_READ' => 'fileB', },

    doesn't make sense, b/c you can only have one key "err" , cause hash-keys are unique!

    update

    honestly I doubt the demonstrated output is really from your code, this doesn't make sense.

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

Re: Remove similar key=value pair from HOH
by pme (Monsignor) on Apr 11, 2015 at 19:33 UTC
    Hi vaibhav07,

    Perl hash unable to store duplicated keys. See and run the code snippet below.

    use Data::Dumper; my %hash =( 'script' => 'foo.pm', 'params' => { 'err' => '99', 'FILE' => 'fileA' }, 'par_global' => { 'err' => '99', # this value (99) is o +verwritten 'err' => '20', # with (20) 'FILE_READ' => 'fileB', }, 'testset' => ['test1'] ); print Dumper( \%hash ) . "\n";
      Hmm, not convinced.
      $ perl -e 'use Data::Dumper; > > my %hash =( > 'script' => 'foo.pm', > 'params' => { > 'err' => '99', > 'FILE' => 'fileA' > }, > 'par_global' => { > 'err' => '99', # this value (99) is + overwritten > 'err' => '20', # with (20) > 'FILE_READ' => 'fileB', > }, > 'testset' => ['test1'] > ); > > print Dumper( \%hash ) . "\n";' $VAR1 = { 'params' => { 'FILE' => 'fileA', 'err' => 99 }, 'script' => 'foopm', 'par_global' => { 'FILE_READ' => 'fileB', 'err' => 20 }, 'testset' => [ 'test1' ] };

      Je suis Charlie.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1123144]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (5)
As of 2024-04-23 18:03 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found