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' };

In reply to Re: Problem in using 'ëxists" function to see if a key exists for a hash by dasgar
in thread Problem in using 'ëxists" function to see if a key exists for a hash by achs

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.