in reply to Re^3: reading a JSON object
in thread reading a JSON object

For future reference: How do I get the size numerically of an array of arrays (AoA)?

Replies are listed 'Best First'.
Re^5: reading a JSON object
by AnomalousMonk (Archbishop) on May 09, 2022 at 23:39 UTC
    ... the size numerically of an array of arrays (AoA) ...

    What does "size" mean in this context? Remember that a Perl array or hash is always and only an array or hash of scalars. However, any of these scalars may be a reference to another array or hash (and a few other things). To find the total number of elements in a nested array/hash structure, you have to figure out a way to recurse through the entire structure from top to bottom and add up the elements in any and all nested arrays/hashes. See the Perl Data Structure Cookbook.

    However, for a simple array, the process is straightforward. An array evaluated in scalar context yields the number of elements in the array. An array identifier or reference dereferenced with the $# sigil yields the index of the highest element in the array.

    Win8 Strawberry 5.8.9.5 (32) Mon 05/09/2022 19:13:22 C:\@Work\Perl\monks >perl use strict; use warnings; my @array = (9, 8, 7); my $arrayref = \@array; print "elements of array: (@array) (@$arrayref) \n"; printf "number of array elements: %d %d \n", scalar(@array), scalar(@$arrayref); print "highest array index: $#array $#$arrayref \n"; ^Z elements of array: (9 8 7) (9 8 7) number of array elements: 3 3 highest array index: 2 2

    But why do you need to explicitly know the "size" of an array or array structure? There are many techniques that allow one to iterate over the elements of an array or recurse through a nested structure without ever knowing the number of elements present. It all depends on just what you are trying to do.


    Give a man a fish:  <%-{-{-{-<

      I have to admit to simply being lazy on this one, since I could have easily looked it up myself. Sorry to bother you with something so trivial.

      I've gotten spoiled by how easy it is to ask questions here. I will try to be more respectful about not wasting people's time.

      Mostly I use perl to create input to spreadsheets, so most of my variables are simple arrays.

      That answers my question. Thanks again for all your help.

      OK, I've tried to look this up myself, but it would take me several hours to figure out what you could write in a few minutes.

      There are 108 rows in $json_aoa, with timestamps from

      $json_aoa->[0][1]

      to

      $json_aoa->[108][1]

      How do I modify the following code

      for my $ar_jentry (@$json_aoa) { # handle each decoded JSON entry printf $fh_data2 "%s\t%s\t%s\t%s\t%s\t%s\t%s\n", @$ar_jentry; }

      so that it only prints values corresponding to the last 10 timestamps, in reverse order (latest to earliest)?

      The other columns in the array are urlkey, timestamp, original, mimetype, statuscode, digest, and length.

      How do I specify an arbitrary subset of these to include in the print out?

      Thanks.

        the last 10 timestamps, in reverse order (latest to earliest)

        See the FAQ How do I sort an array by (anything)?

        How do I specify an arbitrary subset of these to include in the print out?

        I think you are asking how to extract an arbitrary list of fields from a larger array. In that case it is just:

        use strict; use warnings; my @bigarray = qw/a b c d e f g h i j/; my @subset = @bigarray[2, 5, 7]; print "@subset\n";

        Which means @subset will have just those three fields from @bigarray and the output will be "c f h".


        🦛

        Hello anautismobserver,

        didnt read the whole thread but for this specific question, as you started learning perl as I've done, consider the following

        use strict; use warnings; use Data::Dumper; my @AoA = ( [qw(0 Just 0)], [qw(0 Another 0)], [qw(0 Perl 0)], [qw(0 Hacker 0)], ); # see it # print Dumper \@AoA; my @sortedAoA = sort{ $a->[1] cmp $b->[1] # or reverse it comparing $b to $a instead # $b->[1] cmp $a->[1] # or use reverse function to the final array.. } @AoA; # see it # print Dumper \@sortedAoA; # then to just take first X elements use a slice for my $ar_jentry ( @sortedAoA[2..3] ){ print "$ar_jentry->[1]\n" } # output: # Just # Perl # there are other options... # you create a temporary list using sort and then use only first two e +lements with ()[0,1] print "$_->[1]\n" for ( sort{$a->[1] cmp $b->[1]} @AoA )[0,1]; # output: # Another # Hacker

        L*

        There are no rules, there are no thumbs..
        Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.