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

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.

Replies are listed 'Best First'.
Re^7: reading a JSON object
by hippo (Archbishop) on May 11, 2022 at 11:01 UTC
    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".


    🦛

      Thanks. How do I generalize this to two (or more) dimensions?

      For example, the code below selects a subset of the first index (which runs from 0-5).

      How do I modify it so it also selects a subset of the second index (which runs from 0-3)?

      use strict; use warnings; use Data::Dumper qw(Dumper); my $matrix; my @matrix; $matrix[0][0] = 00; $matrix[1][0] = 10; $matrix[2][0] = 20; $matrix[3][0] = 30; $matrix[4][0] = 40; $matrix[5][0] = 50; $matrix[0][1] = 01; $matrix[1][1] = 11; $matrix[2][1] = 21; $matrix[3][1] = 31; $matrix[4][1] = 41; $matrix[5][1] = 51; $matrix[0][2] = 02; $matrix[1][2] = 12; $matrix[2][2] = 22; $matrix[3][2] = 32; $matrix[4][2] = 42; $matrix[5][2] = 52; $matrix[0][3] = 03; $matrix[1][3] = 13; $matrix[2][3] = 23; $matrix[3][3] = 33; $matrix[4][3] = 43; $matrix[5][3] = 53; print Dumper \@matrix; my @matrix2 = @matrix[0, 2]; print Dumper \@matrix2; my @matrix3 = @matrix[0, 1, 5]; print Dumper \@matrix3;

        I'm not quite sure what you're asking, but maybe this will give some insight:

        Win8 Strawberry 5.8.9.5 (32) Wed 05/11/2022 18:46:44 C:\@Work\Perl\monks >perl use strict; use warnings; use Data::Dump qw(dd); my @matrix = ( [ qw(00 01 02 03) ], [ qw(10 11 12 13) ], [ qw(20 21 22 23) ], [ qw(30 31 32 33) ], [ qw(40 41 42 43) ], [ qw(50 51 52 53) ], ); # dd \@matrix; # for debug my @row_set = (0, 2); my @col_set = (3, 0, 1); for my $ar_entry (@matrix[ @row_set ]) { printf "'%s' '%s' '%s' \n", @{$ar_entry}[ @col_set ]; } for my $i_row (@row_set) { printf "row %d: '%s' '%s' '%s' \n", $i_row, @{ $matrix[$i_row] }[ +@col_set ]; } my @sub_matrix = map { [ @{ $_ }[ @col_set ] ] } @matrix[ @row_set ]; # my @sub_matrix = map [ @{ $_ }[ @col_set ] ], @matrix[ @row_set ]; dd \@sub_matrix; ^Z '03' '00' '01' '23' '20' '21' row 0: '03' '00' '01' row 2: '23' '20' '21' [["03", "00", "01"], [23, 20, 21]]


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

Re^7: reading a JSON object -- sort and slicing AoA
by Discipulus (Canon) on May 11, 2022 at 11:12 UTC
    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.
Re^7: reading a JSON object
by AnomalousMonk (Archbishop) on May 11, 2022 at 15:56 UTC