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 :) .
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.