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

Hello:

I need to rewrite some code, and probably almost a complete rewrite at that.

I am getting Arrival and Departure data for an airport, currently in XML, but I can also do JSON. This contains other information besides the flightStatus's (see example):

https://www.dropbox.com/s/w50uliyoxug4r5m/depart.xml.txt

Part of the problem is currently the data is being sorted by XML::Simple by the 3 letter Airport code, and I really need things sorted by Arrival/Departure times.

I admit I am not the best when it comes to working with hashes and arrays, but willing to try and learn if pointed in the right direction.

As I am on a shared server, it is often a bit of work getting things added, so I am working with core modules, plus I have XML::Simple and JSON::XS as well. The only reason I bring this up, is often people here offer up all these other modules that are not part of the core. That is fine if you are on your own server and can easily add what you want. It's not quite that easy on a shared server unfortunately.

Everyone seems to be going the JSON route these days, so probably rewrite in JSON unless there is a reason to go with XML I guess.

If I somewhat understand correctly, the best option would be to load the hash data from the XML/JSON into an Array, which will make sorting easier? Is this the best route to go?

Does anyone know of a good place to start with examples of how to best go about loading things into an array, and then sorting, if that is the best way?

Again, thank you for your help and guidance as always.

John

Comment on
Looking for suggestions, sorting data from XML or JSON
Replies are listed 'Best First'.

A module like JSON::XS is going to return a data-structure. Before we can be more specific than the POD from perldsc on how to get at the data in the structure, we would need to know how that structure is ...um... structured.

JSON or XML? It depends .... XML has, for me, preference in formal communication with external parties because you can document it more verbose and use the xsd for proofing that the messages are according agreements between parties. JSON is more dense and easier for a computer to transform from/to objects, and is more used as a carrying mechanism between server and client (think javascript in browsers that query the back-end.)

For the problem at hand, find below a quick and dirty solution, using XML::Simple, Time:Piece (core module) and some sorting and mapping according the Camel handbook. I can really recommend this book.

Not tested in full and I didn't look for performance! Also be aware for datetime conversions. Check if they are appropriate for your situation. The Data::Dumper is there for testing purposes.

This is only an illustration how to deal with complex structures. First you use Data::Dumper to see how the structure looks like and whether you have hashes or arrays, then you untangle it level by level, adding sorting only at the end. HTH.

“XML vs. JSON” is entirely up to you ... generally, I prefer to make as few “disruptive changes” as possible. Furthermore, in both cases, you wind up at the same place: with (I presume ...) a Perl array of Perl hashrefs. The question before the court has to do with sorting, not with how the structure-to-be-sorted came to be.

As you will see from perldoc -f sort, the sort function takes as one of its parameters a code-block (or a subroutine reference) which does the actual comparison. It returns a value less-than, equal-to, or greater-than zero, and Perl provides two operators, <=> and cmp, to do that, for numeric and string values, respectively. When sorting by more than one field, the || operator is used, as the perldoc specifically shows. Therefore, this is how you sort your data ... no matter how intricate its structure is, and no matter how the Perl data structure that you are sorting came to be.

In the case of XML data, sorting can also be requested using an XPath expression, but not, IIRC, with XML::Simple.