Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Re^7: reading a JSON object -- sort and slicing AoA

by Discipulus (Canon)
on May 11, 2022 at 11:12 UTC ( [id://11143792]=note: print w/replies, xml ) Need Help??


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

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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11143792]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-29 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found