CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Extracting values from Hash

 



rajivcj12
New User

Dec 30, 2015, 12:46 AM

Post #1 of 7 (3258 views)
Extracting values from Hash Can't Post

Hi,

A little help would be much appreciated.

I am trying to extract the key value pairs from a multidiamentional hash that looks link the following


Code
$VAR1 = { 
'result' => [
{
'record' => [
{
'ttl' => '86400',
'Line' => 1,
'type' => ':RAW',
'raw' => '; cPanel first:11.52.1.3 (update_time):1451457515 Cpanel::ZoneFile::VERSION:1.3 hostname:vps-1169473-23827.xxxx.xxxx.com latest:11.52.1.3'
},
{
'ttl' => '86400',
'Line' => 2,
'type' => ':RAW',
'raw' => '; Zone file for mhest.com'
},
{
'ttl' => '14400',
'Line' => 41,
'name' => 'mhest.com.mhest.com.',
'address' => '192.168.5.38',
'class' => 'IN',
'type' => 'A'
},
{
'ttl' => '14400',
'Line' => 42,
'name' => 'mhest.com.',
'address' => '192.168.2.4',
'class' => 'IN',
'type' => 'A'
}
],
'statusmsg' => 'Zone Serialized',
'status' => 1
}
]
};


What I really want to do here is get the line number of the record with the ip 192.168.2.4

I am not really into using hashes. I decoded this from a json output cPanel api produces.


(This post was edited by rajivcj12 on Dec 30, 2015, 1:14 AM)


FishMonger
Veteran / Moderator

Dec 30, 2015, 6:58 AM

Post #2 of 7 (3242 views)
Re: [rajivcj12] Extracting values from Hash [In reply to] Can't Post


Code
foreach my $record ( @{ $VAR1->{result}[0]{record} } ) { 
say "Line: $record->{Line}";
}



rajivcj12
New User

Dec 30, 2015, 7:26 AM

Post #3 of 7 (3237 views)
Re: [FishMonger] Extracting values from Hash [In reply to] Can't Post

I always find my answer from this forum. ALWAYS. You guys are great. Thank you.

Could you please help me understand this.

Here Result is a hash which contains record, statusmsg and status.And Record is a hash in result which contains these other classes. Is it ?

{result}[0]{record} What does the [0] in between mean?


(This post was edited by rajivcj12 on Dec 30, 2015, 7:28 AM)


FishMonger
Veteran / Moderator

Dec 30, 2015, 8:04 AM

Post #4 of 7 (3228 views)
Re: [rajivcj12] Extracting values from Hash [In reply to] Can't Post

Your data structure is a HoAoHoAoH (Hash of Arrays of Hashes of Arrays of Hashes).

[0] is referencing/de-referencing the first element of an array.

'result' => [ is defining a reference to an array. In this case it's a 1 element array and its value is a reference to a hash.

'record' => [ is defining the next level which is an array ref where each element is a reference to a hash.


rajivcj12
New User

Dec 30, 2015, 9:11 AM

Post #5 of 7 (3222 views)
Re: [FishMonger] Extracting values from Hash [In reply to] Can't Post

Okay. Now thats confusing. But I get it.

Thank you again. You saved my day :)


BillKSmith
Veteran

Dec 30, 2015, 3:22 PM

Post #6 of 7 (3206 views)
Re: [rajivcj12] Extracting values from Hash [In reply to] Can't Post

Now that you see how it works, I can go to the next step and answer your original question. The gist of it is to replace FishMonger's for loop with grep to select the required record hash.

Code
my $address = '192.168.2.4'; 
my $param = 'Line';

my $Line =
( grep { exists( $_->{address} ) and $_->{address} eq $address }
@{ $VAR1->{result}[0]{record} } )[0]{$param};
print $Line, "\n";


OUTPUT:

Code
42


The only real problem is that grep returns a list (with only one value in this case). Again we need (...)[0] to select that value.
Good Luck,
Bill


rajivcj12
New User

Dec 30, 2015, 6:44 PM

Post #7 of 7 (3201 views)
Re: [BillKSmith] Extracting values from Hash [In reply to] Can't Post

I have made if work in a foreach loop. The following the code I used.

Code
foreach my $record ( @{ $result->{result}[0]{record} } ) 
{
$address = $record->{address};
if ($address =~ $ip)
{
$line = $record->{Line};
$found +=1;
}
}

And if($found) It proceeds to the next step. Smile

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives