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

Hi Monks, I am stuck with a very simple issue.I need your help to uderstand why the 'exists' function is not identifying a key that is already present. I have an array @portlist which is assigned values using a loop as input,output,inout,inout [2:0], inout [4:0].I am using the following the code to see if the keys of my hash match elements of the portlist:
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;
OUTPUT:
$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' ] };
Please help.

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

    G'day achs,

    You are missing a closing </code> tag which makes your post unreadable. Please fix this (see "How do I change/delete my post?" if you don't know how to do that). Please also fix your title: remove the umlaut and match quotes (see my title).

    I see you've used quotemeta in various places. You may be suffering from this problem:

    $ perl -wE 'my $k = q{a.b}; my %h = ($k, 1); say exists $h{$k} ? 1 : 0 +;' 1 $ perl -wE 'my $k = q{a.b}; my %h = ($k, 1); say exists $h{quotemeta $ +k} ? 1 : 0;' 0

    — Ken

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

    First, your post is extremely difficult to read. It looks like you're missing the </code> tags. I tried reformatting your code and output, which I added at the end of this post.

    It looks like you're iterating over the keys of your hash, but not using the keys to look for anything in the array. I think you might need to change the following line:

    if(exists $orgin{$val})

    to be the following:

    if(exists $orgin{$key})

    Without knowing the contents of the array and hash, it will be difficult to try to help offer suggestions on what could be wrong with your code.


    OP's code and output reformatted:

    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;

    OUTPUT: $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' };
      I updated my question, can anybody please help Even when I am, not using the 'exists' function instead when I am using an 'if' loop just match the array element with the key of the %orgin. The two strings are not matching. Even I print this, the array element and the hash key appears to be the same. I don't understand how to fix this.Please help
        to add a key to the hash orgin if the array element is already present in the hash as a key.

        Don't you mean not present like in your code?

        if (exists $orgin{$val}) { next; } else { print "$val : $key Entered the else loop\n"; push @{$orgin{$val}}, "xxxx"; $count++; }
        poj