use strict; use warnings; use XML::Simple; use Data::Dumper; use Time::Piece; my $xml = XMLin( 'depart.xml', ForceArray => ['flightStatus'], # To force the flightstatuses into an arrayref GroupTags => { # To eliminate unnecessary levels in the structure airlines => 'airline', airports => 'airport', equipments => 'equipment', flightStatuses => 'flightStatus'}); # just to illustrate that you can easily extract the data my $airports = $xml->{appendix}{airports}; my $equipments = $xml->{appendix}{equipments}; my $airlines = $xml->{appendix}{airlines}; my @flightstatuses = @{$xml->{flightStatuses}}; # make it a normal array from flightstatuses print "Airports\n"; print "--------\n"; print Dumper $airports; print "Airlines\n"; print "--------\n"; print Dumper $airlines; print "Equipments\n"; print "----------\n"; print Dumper $equipments; print "Flightstatuses\n"; print "----------\n"; print Dumper (\@flightstatuses); # examples of obtaining an element of a flightstatus that i use in the sorting # take the first element my $flightstatus = $flightstatuses[0]; # obtain departure and arrival times my $departuretime = $flightstatus->{departureDate}{dateUtc}; my $arrivaltime = $flightstatus->{arrivalDate}{dateUtc}; print $departuretime, "\n"; print $arrivaltime, "\n"; # printed after conversion (remark: didn't take the '000Z' in my conversion) print Time::Piece->strptime($departuretime, '%Y-%m-%dT%H:%M:%S.000Z'), "\n"; print Time::Piece->strptime($arrivaltime, '%Y-%m-%dT%H:%M:%S.000Z'), "\n"; # transform the times into epoch, needed for sort function (strictly, it is not, because a Time::Piece # object will stringify to epoch my $departuretime_epoch = Time::Piece->strptime($departuretime, '%Y-%m-%dT%H:%M:%S.000Z')->epoch; my $arrivaltime_epoch = Time::Piece->strptime($arrivaltime, '%Y-%m-%dT%H:%M:%S.000Z')->epoch; print $departuretime_epoch , "\n"; print $arrivaltime_epoch, "\n"; # the classic example of the camel cookbook to sort with mapping where the mappings shown in the examples are used. my @sorted_by_arrival = map {$_->[1]} # map back to original array sort {$a->[0] cmp $b->[0]} # classical compare of two values map {[Time::Piece->strptime($_->{arrivalDate}{dateUtc}, '%Y-%m-%dT%H:%M:%S.000Z')->epoch, $_]} # make a intermediate array with on first place the value used to sort @flightstatuses; my @sorted_by_departure = map {$_->[1]} # map back to original array sort {$a->[0] cmp $b->[0]} # classical compare of two values map {[Time::Piece->strptime($_->{departureDate}{dateUtc}, '%Y-%m-%dT%H:%M:%S.000Z')->epoch, $_]} # make a intermediate array with on first place the value used to sort @flightstatuses; # mapping to be able to print the results my @sorted_by_arrival_id = map {$_->{'flightId'}} @sorted_by_arrival; my @sorted_by_departure_id = map {$_->{'flightId'}} @sorted_by_departure; print join " ", @sorted_by_arrival_id, "\n"; print join " ", @sorted_by_departure_id, "\n";