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

Hi Monks!! This is format of data that I am receiving from SQL object - @result. I need to access @result[0]
for $aref ( @result ) { print "\t [ @$aref ],\n"; } Gives output like ARRAY(0x4b72d28) ARRAY(0x4ba58d8) ARRAY(0x4bdb108) Print Dumper @results; Output looks like one below $VAR1 = [ [ '8959', 'The song by the three rivers', undef, undef ], [ '10710', 'cars by the rodeo drive', '38', undef], [ '10710', 'cars by the rodeo drive', '43', undef] ];
I need to access elements 8959, 10710, 10710 to find their distinct count. For now am not sure how to access them. Any kinds of pointers will be helpful

Replies are listed 'Best First'.
Re: Trying to access array of arrays
by ikegami (Patriarch) on Aug 21, 2017 at 21:24 UTC

    You should pass scalars to Dumper, so

    print Dumper @results;

    should be

    print Dumper \@results;

    This gives

    [ [ [ '8959', 'The song by the three rivers', undef, undef ], [ '10710', 'cars by the rodeo drive', '38', undef], [ '10710', 'cars by the rodeo drive', '43', undef] ] ]

    As you can see, the outer array (@results) contains a single element, your AoA.

    You should therefore use

    my $records = $result[0]; for my $record ( @$records ) { print "\t [ @$record ]\n"; }
Re: Trying to access array of arrays
by shmem (Chancellor) on Aug 21, 2017 at 22:20 UTC
    I need to access elements 8959, 10710, 10710 to find their distinct count.

    To help you, it would be helpful if you posted the code which populates the array @result. This array holds array references, whose first element is the thing you are looking looking for to get the distinct count, right? Well, use them as key to a hash and sum them up:

    my %distinct; for my $aref ( @result ) { $distinct{$aref->[0]}++; } print Dumper(\%distinct);
    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'
Re: Trying to access array of arrays
by AnomalousMonk (Archbishop) on Aug 22, 2017 at 03:15 UTC
Re: Trying to access array of arrays
by thanos1983 (Parson) on Aug 22, 2017 at 08:24 UTC