How do iterate over parts of this complex data structure?

606 Views Asked by At

I have an object that I printed with Data::Dumper:

$VAR1 = {
          'record' => [
                      {
                        'text' => 'booting kernel',
                        'version' => '2',
                        'iso8601' => '2011-06-23 11:57:14.250 +02:00',
                        'event' => 'system booted',
                        'modifier' => 'na'
                      },
                      {
                        'text' => 'successful login',
                        'subject' => {
                                     'sid' => '999',
                                     'uid' => 'user',
                                     'audit-uid' => 'user',
                                     'tid' => '0 0 unknown',
                                     'ruid' => 'user',
                                     'rgid' => 'gsp',
                                     'pid' => '999',
                                     'gid' => 'gsp'
                                   },
                        'version' => '2',
                        'iso8601' => '2011-06-23 11:58:00.151 +02:00',
                        'event' => 'login - local',
                        'return' => {
                                    'retval' => '0',
                                    'errval' => 'success'
                                  },
                        'host' => 'unknown'
                      },
                    ],
          'file' => {
                    'iso8601' => '2011-06-23 11:57:40.064 +02:00'
                  }
        };

I want to print each value navigating such an hash. For what I understood is an hash with two keys (record, file) and record points to an array of hashes.

Can you please help reaching each value of this structure?

I tried:

my @array=$VAR1{'record'};
foreach (@array) {
    print $_{'text'};    
}

… but it does not work.

3

There are 3 best solutions below

0
On BEST ANSWER

$VAR1 is a reference. You need to dereference it. $VAR1->{record} is a reference. You need to dereference it too. $_ is also a reference, so you need to dereference that.

perldoc perlreftut

my @array = @{ $VAR1->{'record'} };
foreach (@array) { 
    print $_->{'text'}; 
}
1
On

$VAR1{record} is an array reference, not an array. To get to the array, you need to dereference:

my @array = @{ $VAR1->{record} };

Each element in the array is a hash reference, so:

for my $record ( @array ) {
    print $record->{text};
}
0
On

If you just want to iterate over it, you can do something like this:

iterate($VAR1);

sub iterate {
    my $input = shift;
    unless (ref $input) {
        print "$input\n";
    } elsif (ref $input eq 'ARRAY') {
        iterate($_) for @$input;
    } elsif (ref $input eq 'HASH') {
        for (keys %$input) {
            print "$_\n";
            iterate($input->{$_});
        }
    } else {
        print ref $input,"\n";
    }
}

This doesn't exactly pretty print it like Data::Dumper does, but the technique might be useful if you want to do anything else with an arbitrary nested structure you don't know much about.