Printing information from perl arrays/hashes -
i trying access data returned api, cant correct values out of array, know api returning data dumper can print out on screen no problem.
when trying print information array know print out receiving hash. sorry if confusing, still learning.
using following code getting below output,
foreach $hash (@{$res->data}) { foreach $key (keys %{$hash}) { print $key, " -> ", $hash->{$key}, "\n"; } }
output
stat -> hash(0xf6d7a0) gen_info -> hash(0xb66990)
do of know how can modify above traverse in hashes?
the bottom line of trying print out value array.
please see dumper of array.
print dumper(\$res->data);
http://pastebin.com/raw.php?i=1dejzx2f
the data trying print out guid field.
i thought like
print $res->data->[1]->{guid}
but doesn't seem work, i'm sure i'm missing here , thinking more should, if point me in write direction or write me correct print , explain doing wrong great
thank you
the structure have array of hashes of hashes. shown in dump
# first hash key being 'stat', # second hash keys (traffic, mail_resps...) followed values (=> 0) 'stat' => { 'traffic' => '0', . 'mail_resps' => '0',
so value of keys in first hash hash or hash of hashes.
if want print out every element, need add additional loop keys of second hash.
foreach $hash (@{$res->data}) { # each item in array/list foreach $key (keys %{$hash}) { # keys first hash (stat,gen_info) foreach $secondkey ( keys %{$hash->{$key}}) # keys second hash { print $key, " -> ", $secondkey, " -> ",${$hash->{$key}}{$secondkey}, "\n"; } } }
if interested in guid, access as:
$res->data->[1]->{gen_info}{guid}
where gen_info key first hash , guid key second hash
you can check see if keys exists in first , second hash before access using exits
$n = 1 # index of array want information if (( exists $res->data->[$n]->{gen_info} ) && # check keys exists in ( exists $res->data->[$n]->{gen_info}{guid} )) # in each hash { # need } else { print "error: either gen_info or guid not exist\n"; }
Comments
Post a Comment