Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

phew...that was a mouthful

Yesterday Steves was kind enough to provide me with the following code:
I've changed it slightly but it's essentially the same

foreach (@log_file) { chomp; next if ($_ =~ /^\s*$/); if ($_ =~ /^Date:\s+(.+)$/) { push(@lookup, $hash) if (defined($hash)); $hash = {DATE => $1}; } if ($_ =~ /^SServer:\s+(.+)$/) { $hash->{SSERVER} = $1; } elsif ($_ =~ /^Device:\s+(.+)$/) { $hash->{DEVICE} = $1; } elsif ($_ =~ /^CString:\s+(.+)$/) { $hash->{CSTRING} = $1; } elsif ($_ =~ /^Port:\s+(.+)$/) { $hash->{PORT} = $1; } elsif ($_ =~ /^SNMP Traps Generated:/) { $hash->{TRAPS} = {TRAP_LIST => []}; } elsif ($_ =~ /^\s+Trap OID:\s+(.+)$/) { $hash->{OID} = $1; } elsif ($_ =~ /^\s+Generic:\s+(.+)$/) { $hash->{GENERIC} = $1; } elsif ($_ =~ /^\s+Specific:\s+(.+)$/) { $hash->{SPECIFIC} = $1; } elsif ($_ =~ /^\s+Varbind:\s+(.+)$/) { $trap = {VARBIND => $1}; } elsif ($_ =~ /^\s+Type:\s+(.+)$/) { $trap->{TYPE} = $1; } elsif ($_ =~ /^\s+Data:\s+(.+)$/) { $trap->{DATA} = $1; push(@{$hash->{TRAPS}->{TRAP_LIST}}, $trap); } } push(@lookup, $hash) if (defined($hash)); print $lookup[0]{'SSERVER'}; print $lookup[0]{'DEVICE'}; print $lookup[0]{'TRAPS'}->{'TRAP_LIST'}; #returns an array ref
Any idea how I can print element 0 of the lookup array containing the hash of arrays?
Thanks

Replies are listed 'Best First'.
Re: How do I print an array of hashes containing an array
by jasonk (Parson) on Feb 18, 2003 at 15:36 UTC

    If you just want to print the contents of the arrayref (and the contents of that array are all simple data types), you only need to deref it:

    print join(', ', @{$lookup[0]{'TRAPS'}->{'TRAP_LIST'}})."\n";

    If you want to print the entire contents of a deep structure, check out Data::Dumper.

      This is my problem.
      When running your code I get:
      HASH(0x22cad04), HASH(0x22caac4), HASH(0x22caa88), HASH(0x22c6928)

        Then use Data::Dumper to set a better look at that's actually going in to the ref, then go from there.

        Check out perldsc if you haven't already. Good luck to you.

        John J Reiser
        newrisedesigns.com