perlreferencedata-dumper

How do iterate over parts of this complex data structure?


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.


Solution

  • $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'}; 
    }