Perl Array within an hash


 
Thread Tools Search this Thread
Top Forums Programming Perl Array within an hash
# 1  
Old 04-29-2013
Perl Array within an hash

Hi All

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

Code:
$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


Code:
#!/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
# 2  
Old 04-29-2013
You are putting a hash ref into an array as the only element with this line
Code:
push @myarray,$perl_data->{'data'};

Instead try examining the structure the JSON decode module returns.
Code:
$perl_data # a hashref
$perl_data->data # an array ref to an array of hashes

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

# 3  
Old 04-29-2013
thanks that worked, list a treat, just two questions, if i wanted other fileds as well could i do this,

Code:

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

and also i am getting lots of these errors,

Code:
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

Last edited by Corona688; 04-29-2013 at 01:20 PM..
# 4  
Old 04-29-2013
Quote:
Originally Posted by ab52
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}";
}
It's non-destructive code, try it and see what happens.
Quote:
Originally Posted by ab52
and also i am getting lots of these errors,

Code:
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
You could try checking that hostname is set, eg.
Code:
   print "$host->{hostname}\n" if defined $host->{hostname};

# 5  
Old 04-29-2013
Thanks

Last qustion Smilie

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
# 6  
Old 04-29-2013
Use Net::Ping.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Perl hash of hashes anonymous array

Hello experts. I'm having problems with a snippet of code. I was hoping to get help/advice to correct. A file that this script parses has changed to the point where I can no longer use a scalar, it looks as though I need to create an array for a hash of hashes below. The first output of... (1 Reply)
Discussion started by: timj123
1 Replies

2. Shell Programming and Scripting

Perl : Assigning multile hash values to a single array

I know that @food = %fruit; Works. But how do I assign %fruit and %veggies to @food ? (2 Replies)
Discussion started by: popeye
2 Replies

3. Shell Programming and Scripting

Compare values of hashes of hash for n number of hash in perl without sorting.

Hi, I have an hashes of hash, where hash is dynamic, it can be n number of hash. i need to compare data_count values of all . my %result ( $abc => { 'data_count' => '10', 'ID' => 'ABC122', } $def => { 'data_count' => '20', 'ID' => 'defASe', ... (1 Reply)
Discussion started by: asak
1 Replies

4. Shell Programming and Scripting

array of hash - perl

How do I get the unique hashes from an array of hashes? @ar1 = ( {a=>1,b=>2}, {c=>3,d=>4},{a=>1,b=>2});I need : @ar2 = ( {a=>1,b=>2}, {c=>3,d=>4});Thanks. (2 Replies)
Discussion started by: shellwell
2 Replies

5. Shell Programming and Scripting

perl Can't coerce array into hash at

Hi guys I have this part of a perl script that returns and odd error if ($args{software}) { print " @DISTFILE_GROUPS $output->{distfile_groups}->{ get_rdist_groups}\n"; and the error is Can't coerce array into hash at i've never seed this error before, any ideas thanks... (0 Replies)
Discussion started by: ab52
0 Replies

6. Shell Programming and Scripting

perl - need help with 2 arrays to hash or 2d array?

I have 2 arrays: @array1 outputs the following: 1 1 1 2 @array2 outputs the following A B C D (2 Replies)
Discussion started by: streetfighter2
2 Replies

7. Shell Programming and Scripting

Array of hash in perl does not work

Hi , I have an input.txt file that i read node: id= c1, class=nb, cx=100, cy=100, r=10 node: id=c2, class=b, cx=150, cy=130, r=10 node: id=c3, class=nb, cx=50, cy=80, r=10 node: id=c4, class=nb, cx=120, cy=200, r=10 i split over , and = to create a global array and then passed it to a... (6 Replies)
Discussion started by: rsanjay
6 Replies

8. Shell Programming and Scripting

PERL, push to hash of array problem

$key = "a"; $value = "hello"; %myhash = {} ; push @{ myHash{$key} }, $hello; print $myHash{$key}."\n"; this script prints "hello" but has following error message. Reference found where even-sized list expected at ./test line 5. can any one help me to fix this problem?? (3 Replies)
Discussion started by: bonosungho
3 Replies

9. Shell Programming and Scripting

perl array question from going through hash

suppose my @{$data1{$callid}}; cotains one two three three five six one two three of random patterns but each item is separated by white space or tab, Below code extract and get rid of the whitespace perfectly so that it shows now like this onetwothree threefivesix... (2 Replies)
Discussion started by: hankooknara
2 Replies

10. Shell Programming and Scripting

hash,array and perl

Hi,i have a code fragment below. %tag = (); #line 1 $tag{'info'} = $datastring; #line 2 $resp = $ua->request( #$ua is a user agent POST 'http://10.2.3.0' , Content_Type => application/x-www-form-urlencoded Content => #line 3 I am not sure of what the code... (3 Replies)
Discussion started by: new2ss
3 Replies
Login or Register to Ask a Question