Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Sorting complex data

by techie411 (Acolyte)
on Feb 13, 2013 at 19:31 UTC ( [id://1018610]=perlquestion: print w/replies, xml ) Need Help??

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

I have data from JSON using Data::Dumper that looks like the following when I -
print Dumper $json_response; $VAR1 = { 'report' => [ { 'stat' => 1, 'timestamp' => '1360775722', 'status' => '"Active"', 'name' => '"09:15 am, 13 Feb, 2013"', 'id' => '200' }, { 'stat' => 1, 'timestamp' => '1360775806', 'status' => '"Active"', 'name => '"09:16 am, 13 Feb, 2013"', 'id' => '199' } ], 'username' => 'test' };
Is this type of data a hash with anonymous array? I am able to grab each key, value doing:
foreach ( @{$json_response->{report}} ){ my %report; $report{status} = $_->{status}; $report{id} = $_->{id}; print "CGGSID: $report{id}\n"; print "Status: $report{status}\n\n"; }
How would I sort the data by the 'id'? I know I can push the 'id' to an array and probably sort the array, but trying to see other, more efficient way for sorting this. Thank you!

Replies are listed 'Best First'.
Re: Sorting complex data
by davido (Cardinal) on Feb 13, 2013 at 19:47 UTC

    I'm assuming that you're interested just in the "report" sub-structure, so I'm only sorting that part. And I'm assuming that you want a numerical sort for ID's.

    use strict; use warnings; use Data::Dumper; my $json_response = { 'report' => [ { 'stat' => 1, 'timestamp' => '1360775722', 'status' => '"Active"', 'name' => '"09:15 am, 13 Feb, 2013"', 'id' => '200' }, { 'stat' => 1, 'timestamp' => '1360775806', 'status' => '"Active"', 'name' => '"09:16 am, 13 Feb, 2013"', 'id' => '199' } ], 'username' => 'test' }; my @sorted = sort { $a->{id} <=> $b->{id} } @{ $json_response->{report +} }; print Dumper \@sorted;

    Dave

Re: Sorting complex data
by duelafn (Parson) on Feb 13, 2013 at 22:43 UTC

    I like Sort::Key for this:

    use Sort::Key qw/ nkeysort /; foreach ( nkeysort { $_->{id} } @{$json_response->{report}} ) { # ... }

    Good Day,
        Dean

Re: Sorting complex data
by sundialsvc4 (Abbot) on Feb 13, 2013 at 20:18 UTC

    To clarify the above ... the sort verb allows a comparison subroutine to be specified.   That can be as simple as the “one-liner” in-between (and including) the curly braces, which uses the <=> operator designed expressly for this purpose, or it can be a much more complicated function.   Within that function, for your further convenience, the variables $a and $b are magically defined.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (9)
As of 2024-04-23 07:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found