Perl Array within an hash

Hi All

I have been using a curl code to output an hash that looks like this

$VAR1 = {                                                                                                                                                                                           
          'data' => [                                                                                                                                                                               
                      {                                                                                                                                                                             
                        'rx_dropped.d' => 0,                                                                                                                                                        
                        '_tx_retries' => 0,                                                                                                                                                         
                        '_rx_crypts' => 0,                                                                                                                                                          
                        'rx_packets' => 3964,                                                                                                                                                       
                        '_tx_bytes' => 2381503,                                                                                                                                                     
                        'rx_crypts.d' => 0,                                                                                                                                                         
                        'tx_rate' => 11000,                                                                                                                                                         
                        '_id' => '511a6aed9309b0e4a750454a',                                                                                                                                        
                        'state_pwrmgt' => bless( do{\(my $o = 0)},                                                                                                    
                        '_rx_errors' => 0,                                                                                                                                                          
                        'hostname' => 'android-0016DC6CADE6',                                                                                                                                       
                        'essid' => 'subway',                                                                                                                                                        
                        '_rx_bytes' => 543679,                                                                                                                                                      
                        'is_guest' => $VAR1->{'data'}[0]{'state_pwrmgt'},                                                                                                                           
                        'first_seen' => 1360685805,                                                                                                                                                 
                        'mac' => '00:16:dc:6c:ad:e6',                                                                                                                                               
                        'authorized' => bless( do{\(my $o = 1)},                                                                                                     
                        'is_11n' => $VAR1->{'data'}[0]{'authorized'},                                                                                                                               
                        'bytes.r' => 3,                                                                                                                                                             
                        'rx_errors' => 0,                                                                                                                                                           
                        'rx_dropped.r' => 0,                                                                                                                                                        
                        'ap_mac' => 'dc:9f:db:1a:37:c1',                                                                                                                                            
                        'user_id' => '511a6aed9309b0e4a750454a',                                                                                                                                    
                        'rx_errors.d' => 0,                                                                                                                                                         
                        'tx_retries' => 0,                                                                                                                                                          
                        'rx_rate' => 1000,                                                                                                                                                          
                        '_tx_errors' => 0,                                                                                                                                                          
                        'rx_dropped' => 0,                                                                                                                                                          
                        'rx_crypts.r' => 0,                                                                                                                                                         
                        'bytes' => 2927100,                                                                                                                                                         
                        'ip' => '10.33.1.140',                                                                                                                                                      
                        'bytes.d' => 248,                                                                                                                                                           
                        'channel' => 1,                                                                                                                                                             
                        'is_11b' => undef,                                                                                                                                                          
                        'bssid' => 'dc:9f:db:1c:37:c1',                                                                                                                                             
                        'rx_packets.d' => 2,                                                                                                                                                        
                        'rx_packets.r' => 0,                                                                                                                                                        
                        'tx_packets' => 3785,                                                                                                                                                       
                        'tx_errors.d' => 0,                                                                                                                                                         
                        'roam_count' => 1,                                                                                                                                                          
                        '_tx_packets' => 3783,                                                                                                                                                      
                        'tx_bytes.d' => 46,                                                                                                                                                         
                        'radio' => 'ng',                                                                                                                                                            
                        'rx_frags.d' => 0,                                                                                                                                                          
                        'signal' => -58,                                                                                                                                                            
                        'tx_bytes.r' => 0,                                                                                                                                                          
                        'dhcpend_time' => 0,                                                                                                                                                        
                        'is_11a' => undef,                                                                                                                                                          
                        'rx_frags.r' => 0,                                                                                                                                                          
                        'state' => 15,                                                                                                                                                              
                        '_uptime' => 5196,                                                                                                                                                          
                        '_rx_dropped' => 0,                                                                                                                                                         
                        'tx_dropped.r' => 0,                                                                                                                                                        
                        'uptime' => 5336,                                                                                                                                                           
                        'last_seen' => 1367228813,                                                                                                                                                  
                        'map_id' => '50b548ed50cab0e4dc11a3c2',                                                                                                                                     
                        't' => 'sta',                                                                                                                                                               
                        'qos_policy_applied' => $VAR1->{'data'}[0]{'authorized'},                                                                                                                   
                        'dhcpstart_time' => 871307111,                                                                                                                                              
                        'rx_frags' => 0,                                                                                                                                                            
                        'rx_bytes.d' => 202,                                                                                                                                                        
                        'rx_errors.r' => 0,                                                                                                                                                         
                        'state_ht' => $VAR1->{'data'}[0]{'authorized'},                                                                                                                             
                        'auth_time' => 0,                                                                                                                                                           
                        'tx_retries.r' => 0,                                                                                                                                                        
                        'rx_bytes' => 545287,                                                                                                                                                       
                        'tx_retries.d' => 0,                                                                                                                                                        
                        'tx_packets.d' => 1,                                                                                                                                                        
                        'assoc_time' => 1367223477,                                                                                                                                                 
                        'tx_packets.r' => 0,                                                                                                                                                        
                        'ccq' => 683,                                                                                                                                                               
                        'rx_bytes.r' => 2,                                                                                                                                                          
                        'tx_power' => 40,                                                                                                                                                           
                        '_rx_packets' => 3955,                                                                                                                                                      
                        'rssi' => 23,                                                                                                                                                               
                        'tx_bytes' => 2381813,                                                                                                                                                      
                        '_rx_frags' => 0,                                                                                                                                                           
                        'rx_crypts' => 0,                                                                                                                                                           
                        'tx_errors.r' => 0,                                                                                                                                                         
                        'tx_dropped.d' => 0,                                                                                                                                                        
                        'oui' => 'Archos',                                                                                                                                                          
                        'tx_dropped' => 0,                                                                                                                                                          
                        'idletime' => 15,                                                                                                                                                           
                        'tx_errors' => 0,                                                                                                                                                           
                        'powersave_enabled' => $VAR1->{'data'}[0]{'authorized'},                                                                                                                    
                        '_tx_dropped' => 0,                                                                                                                                                         
                        'noise' => -81                                                                                                                                                              
                      },                                                                                                                                                                            
                      {                                                                                                                                                                             
                        'rx_dropped.d' => 0,                                                                                                                                                        
                        '_tx_retries' => 0,                                                                                                                                                         
                        '_rx_crypts' => 0,                                                                                                                                                          
                        'rx_packets' => 2932,                                                                                                                                                       
                        '_tx_bytes' => 930096,                                                                                                                                                      
                        'rx_crypts.d' => 0,                                                                                                                                                         
                        'tx_rate' => 117000,                                                                                                                                                        
                        '_id' => '50cf1ab950cab0e4191aa4c2',                                                                                                                                        
                        'state_pwrmgt' => $VAR1->{'data'}[0]{'state_pwrmgt'},                                                                                                                       
                        '_rx_errors' => 0,                                                                                                                                                          
                        'hostname' => 'Mark-Computer',                                                                                                                                              
                        'essid' => 'subway',                   

and so on

I am trying to find a way of looping through the hash and array and getting just the host name

so far i have this

#!/usr/bin/perl

use LWP::Simple;                
use JSON qw(encode_json decode_json);
use Data::Dumper;               
use strict;                     
use warnings;                   
use File::Slurp qw(read_file write_file);


my $json_data = `curl --sslv3  --silent --cookie /tmp/unifi_cookie  --cookie-jar /tmp/unifi_cookie --insecure --data "login=login" --data "username=admin"  --data "password=password" --data "json={}" https://wifi1:8443/api/stat/sta`; 
my $json_obj = new JSON;
my $perl_data = $json_obj->decode($json_data);

my @myarray; 
push @myarray,$perl_data->{'data'};


foreach my $value (@myarray) {
        foreach my $hvalue (keys %{$value}) {
            print $hvalue . "\t";
            print $value->{$hvalue} . "\n";
        }

}


any help woiuld be great

You are putting a hash ref into an array as the only element with this line

push @myarray,$perl_data->{'data'};

Instead try examining the structure the JSON decode module returns.

$perl_data # a hashref
$perl_data->data # an array ref to an array of hashes

so

for my $host (@{$perl_data->{data}}){
   print "$host->{hostname}\n";
}

thanks that worked, list a treat, just two questions, if i wanted other fileds as well could i do this,


for my $host (@{$perl_data->{data}}){
   print "$host->{hostname}\n";
   print "$host->{oui}";
}

and also i am getting lots of these errors,

Use of uninitialized value in concatenation (.) or string at ./tester line 20


e,g it will give me some hostname and then that line, or blank lines

Thanks
A

It's non-destructive code, try it and see what happens.

You could try checking that hostname is set, eg.

   print "$host->{hostname}\n" if defined $host->{hostname};

Thanks

Last qustion :stuck_out_tongue:

I have managed to read in the list of hosts into a file, i wanna see if i can ping the and then ones i can i want to use later for something else

Adam

Use Net::Ping.