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

Can anyone tell me why I can't access "1" from the following data structure:
$VAR1 = { 'response_1.3.6.1.2.1.2.2.1.12.27' => { 'Time_Arrived' => '16:14:13.8', 'Error_Status' => 'No error', '1.3.6.1.2.1.2.2.1.12.27' => [ [ 'ifInNUcastPkts.27', 1 ] ] }, 'request_1.3.6.1.2.1.2.2.1.12.20' => { 'Time_Arrived' => '16:14:15.8', 'Error_Status' => 'No error', '1.3.6.1.2.1.2.2.1.12.20' => [ [ 'ifInNUcastPkts.20', 1 ] ] },
using the following code returns "ifInNUcastPkts.27 1"
print "@{$key_hash{$key}{$oid}[0]}\n";

Replies are listed 'Best First'.
Re: Acessing an array element in a hash
by dragonchild (Archbishop) on Mar 26, 2003 at 15:23 UTC
    @{$key_hash{$key}{$oid}[0]} is an array. You can tell that by the '@' on the left. You're close, those. You just need to go one more level deep.

    $key_hash->{$key}{$oid}[0][1] will do you just nicely.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Acessing an array element in a hash
by Mr. Muskrat (Canon) on Mar 26, 2003 at 15:25 UTC
Re: Acessing an array element in a hash
by artist (Parson) on Mar 26, 2003 at 15:27 UTC
    Missing Lines:
    my %key_hash= %{$VAR1}; $key = 'response_1.3.6.1.2.1.2.2.1.12.27'; $oid = '1.3.6.1.2.1.2.2.1.12.27';

    And now the code which should give you '1'
    print $key_hash{$key}{$oid}[0]->[1];
    Possible Error Detection:
    $key_hash{$key}{$oid}[0] contains a single reference to an Array. So your code
    print "@{$key_hash{$key}{$oid}[0]}\n";
    dereference it and prints the entire array. Above suggested code just prints '1' as the 2nd element of that dereferenced array'.

    artist

      Thanks guys,
      A simple mistake as always, and yes Mr_Muskrat I should and will be opening an account.
Re: Acessing an array element in a hash (that time)
by tye (Sage) on Mar 26, 2003 at 17:44 UTC

    It's been a while, so I'll note that references quick reference can be very valuable in making it much easier to remember how to solve problems like this.

                    - tye
Re: Acessing an array element in a hash
by Tomte (Priest) on Mar 26, 2003 at 15:21 UTC
    Edit:Silly me, dragonchild is right and I should read what I write, not just copy & paste; corrected that character :-)

    As far as I understand this on a first glance, you're printing an array, so thats normal behaviour you're showing (assuming I'm right on my assumptions on what values $key_hash, $key, $oid carry).

    I guess you want to access element 1 in the nested array you're dereferencing with something like
    @${$key_hash{$key}{$oid}[0]}[1] to get to the 1.

    regards,
    tomte


      No. That's an array-slice. q.v. my reply in this thread to see the correct answer.

      ------
      We are the carpenters and bricklayers of the Information Age.

      Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.

      Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Acessing an array element in a hash
by OM_Zen (Scribe) on Mar 26, 2003 at 16:43 UTC
    Hi

    The code to populate the hash assuming is like the below , you can access the elements of the array of the hash of the hash like this
    my $ascalar = { 'a' => {'a' =>'valueofhashofhash', 'hashinsideahash' => [1, 'secondelementofarrayofsecondhash']}}; print "[$ascalar->{'a'}->{'hashinsideahash'}[0]]\n"; print "[$ascalar->{'a'}->{'hashinsideahash'}[1]]\n"; __END__ [1] [secondelementofarrayofsecondhash]