achs has asked for the wisdom of the Perl Monks concerning the following question:
OUTPUT:foreach my $key(keys %orgin) { #my $tmp_key=quotemeta "$key"; foreach my $val(@port_list) { $val=~s/\s+$//; #print "$val***********"; #my $tmp_val=quotemeta "$val"; if(exists $orgin{$val}) { #print "same key :$tmp_val\n"; next; } else { print "$val : $key Entered the else loop\n"; push @{$orgin{$val}}, "xxxx"; $count++; } } } print Dumper \%orgin;
Please help.$VAR1 = { '' => [ 'xxxx' ], ' input' => [ 'xxxx' ], 'input' => [ 'ai400_INH', 'i50_TXD_p' ], 'inout' => [ 'VIORING50', 'DVCC16', 'VPRE30', 'VCC50_p', 'VIO50_p', 'io50_MISO_p', 'io50_NCS_p', 'io50_RXD_p', 'aio33_atb_0', 'aio33_atb_3' ], 'output' => [ 'ao400_INH_p' ], ' inout' => [ 'xxxx' ], 'inout [4:0]' => [ 'VIO50' ], ' inout [2:0]' => [ 'xxxx' ], ' output' => [ 'xxxx' ], ' inout [4:0]' => [ 'xxxx' ] };
The problem I am facing with the code above is I have a few a elements in the array @port_list which I get by reading a input file. I am trying to add a key to the hash orgin if the array element is already present in the hash as a key. Here @port_list contains input, inout, output, inout[4:0], inout[2:0] %orgin contains the keys, input, inout, output, inout4:0.So the code if works, the %orgin will have a new key inout2:0 with a value 'xxxx' added to it with changing the values of the already exisiting keys and values.
Can anybody help me understand why the keys are getting overwritten though it is already there?Any help will be hugely apprecaited.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Problem in using 'exists' function to see if a key exists for a hash
by kcott (Archbishop) on Feb 17, 2016 at 06:38 UTC | |
|
Re: Problem in using 'ëxists" function to see if a key exists for a hash
by dasgar (Priest) on Feb 17, 2016 at 06:43 UTC | |
by achs (Initiate) on Feb 17, 2016 at 08:34 UTC | |
by poj (Abbot) on Feb 17, 2016 at 08:49 UTC | |
by achs (Initiate) on Feb 17, 2016 at 08:52 UTC | |
by poj (Abbot) on Feb 17, 2016 at 09:34 UTC | |
|