Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?

Re: Looking for suggestions, sorting data from XML or JSON

by sundialsvc4 (Abbot)
on Jul 06, 2013 at 19:08 UTC ( #1042938=note: print w/replies, xml ) Need Help??

in reply to Looking for suggestions, sorting data from XML or JSON

“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.

  • Comment on Re: Looking for suggestions, sorting data from XML or JSON

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1042938]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (3)
As of 2017-11-25 06:57 GMT
Find Nodes?
    Voting Booth?
    In order to be able to say "I know Perl", you must have:

    Results (355 votes). Check out past polls.