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 | |
|
Re: Remove similar key=value pair from HOH
by pme (Monsignor) on Apr 11, 2015 at 19:33 UTC | |
by Laurent_R (Canon) on Apr 11, 2015 at 22:46 UTC | |
by LanX (Saint) on Apr 12, 2015 at 00:09 UTC | |
by AnomalousMonk (Archbishop) on Apr 12, 2015 at 00:27 UTC | |
by choroba (Cardinal) on Apr 12, 2015 at 00:45 UTC | |
| |
by vaibhav07 (Acolyte) on Apr 12, 2015 at 04:29 UTC | |
by pme (Monsignor) on Apr 12, 2015 at 07:25 UTC | |
by AnomalousMonk (Archbishop) on Apr 12, 2015 at 07:24 UTC | |
by Laurent_R (Canon) on Apr 12, 2015 at 09:47 UTC | |
|