Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??

Hello-- I have solved my problem, but I don't know how. (I have kept it below for ...posterity?) What's going on here? :) When I change my sort function to the following it works as expected, sorting the dates. What is the difference between parentheses absent and parentheses present?

Working code:

sub sortByDate { #get dates. will look like this: date:"2015-02-16", date:"YYYY-MM-DD +", my ($aDate) = $a =~ /date:\"(\d{4}\-\d{2}\-\d{2})"/; my ($bDate) = $b =~ /date:\"(\d{4}\-\d{2}\-\d{2})"/; return ($aDate cmp $bDate); }

Original post:


I am newish to Perl and trying to sort a file but it is behaving unexpectedly. I have a file which has several items in json format (not in date order and with some blank lines), for example:

{ date:"2015-03-01", content:"asdf" } { date:"2015-05-01", content:"erwa" } { date:"2015-01-02", content:"erts" } { date:"2014-04-02", content:"w34r" }

when I run my code intended to sort by date, it seems to sort the file in another way which I don't quite understand. It puts all the blank lines first, and then all the other lines stay in the order they were in the file. Here is my code:

#!/user/bin/perl use strict; use warnings; # open file open (MYFILE, '<jsonfile.json') or print ("Can't open file."); # pull into list my @events = <MYFILE>; # close file close (MYFILE); # organize by date sub sortByDate { #get dates. will look like this: date:"2015-02-16", date:"YYYY-MM-DD +", my $aDate = $a =~ /date:\"(\d{4}\-\d{2}\-\d{2})"/; my $bDate = $b =~ /date:\"(\d{4}\-\d{2}\-\d{2})"/; return ($aDate cmp $bDate); } @events = sort sortByDate @events; &printDates; <code> <p>When run, I get</p> <code> 2015-03-01 2015-05-01 2015-01-02 2014-04-02
Any help would be appreciated. I am new to custom sorts and regex.

In reply to sorting a file by a date:"YYYY-MM-DD" field with cmp by jason.printer

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-05-25 01:23 GMT
Find Nodes?
    Voting Booth?
    Do you prefer to work remotely?

    Results (84 votes). Check out past polls.