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.

In reply to 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.