Hi Monks, I would like to convert a array of hash ref to hash and check a value in it. This is how it goes. The following code gets the array reference in $ams_hosts

my $ams_hosts = $request->get_config()->get_ams_hosts($domain);
The following code
print STDERR Dumper @{$ams_hosts};
prints this
$VAR1 = { 'domainId' => '1', 'deviceIp' => '172.16.133.56', 'deviceType' => 3, 'deviceName' => 'AMS' }; $VAR2 = { 'domainId' => '1', 'deviceIp' => '172.16.133.57', 'deviceType' => 3, 'deviceName' => 'ams_57' };
Now i would like to check if a scalar variable, say $ams_ip=172.16.133.52, does not exist in the above hash assign undef to $ams_ip. I thought of assigning the array to hash with (key,value) as (deviceIp,<its correspponding ip address like 172.16.133.56> ) I tried the following snippet
my %ams_ips_hash = map {$_->{'deviceIp'}++} @{$ams_hosts}; $ams_ip = "" if !exists $ams_ips_hash{ $ams_ip};
but it doesnt work. Please let me know what I am doing wrong and what would make it right. As I am new to perl, I would be obliged to learn from perl monks like you. Your help is highly appreciated. Thanks in advance :) .


In reply to Converting array of hash references to hash by anirudh.agarwal

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.