I'm having trouble wrapping my brain around the data structure that is being returned...
You've already accepted an answer, I'm just going to clarify the interpretation of the Data::Dumper
output:
- Each
{...}
means a reference to a hash. You'll see key => value,
as hash elements.
- Each
[...]
represents a reference to an array. You'll see value,
as array elements.
Breaking apart what you have:
$VAR = $VAR1 = {
'results' => [
.... # This is an array reference
]
Or
$VAR->{results} = [];
This is a hash with a single key of results
. The hash has a reference to an array as it's value. So far:
$VAR1 = {
'results' => [ # This is the [0] element in my array
{
... # This is a hash reference
}
]
[ # This is the [1] element in my array
{
... # This is a hash reference
}
In this array, there are two values, each value is pointing to a hash reference:
$VAR->{results}->[0] = {};
$VAR->{results}->[1] = {};
In each of these arrays, the hash reference has 12 keys and values:
- admin
- dataflow
- devices_online
- object
- ifDescr
- ifDevice
- ifIndex
- oper
- percent_online
- Map
- These are has references....
- total_devices
- devices_offline
The first 10 are simply key/value pairs. The last two are references to a further hash with a single key/value pair. The key is null. I assume this is a mistake of some sort.
Now I can refer to one of these items like this:
$VAR->{results}->[1]->{ifIndex} = 'DUMMY DATA';
Assuming the current structure, here's a way to refer to it in a loop:
my $VAR = some_function() # Returns a reference to a hash.
for my $result ( keys %{ $VAR } ) { # Dereference the hash reference...
say "Key for results is '$result'"; # Only one result. And that's 'result'...
my @array = $VAR->{$result}; # Dereference the array reference that hash points to
for my $element ( 0..$#array ) { # Now we get to the two elements in the array
say qq(Looking at element #$element);
my $hash_key = $array[$element]; # he hash reference that the array points to
my %inner_hash = %{ $hash_key }; # Another dereference...
for my $key ( keys %inner_hash" ) {
say "\$VAR->{$result}->[$element]->{%hash_key} = "
. $VAR->{$result}->[$element]->{%hash_key};
}
}
}
This won't entirely work because total_device
and device_offline
are again hash references. I should make an exception in my inner most loop, and if either of these are the key to my inner hash, I need to do another dereference to get to the hash. I'll let you work that out.
Of course, I know my structure, so I could write a program structure to handle it. If I didn't know the layout of my data structure, I would have to use the ref
command to find out if I'm referring to a hash or an array, and dereference and loop accordingly. This is pretty much what Data::Dumper
does.
I usually assume that such a complex structure comes from a class constructor, and I would expect to see the blessed class name of the object in such a Data::Dumper dump. In that case, I would tell you to use the methods for that class and not to deconstruct the data structure and munge it on your own. That's a no-no in object oriented design.
You should always treat the data structure is a black box. You should not be peaking through the windows just because Perl doesn't provide blinds for marking the structure and methods as private. It's still bad manners.
However, Data::Dumper didn't show the name of the class, so it's not an class object. Ogle away at the data structure.
Take a look at the Perl Reference Tutorial to see if that helps clarify the situation for you.