Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Regex/Sort Questions

by logie17 (Friar)
on Dec 05, 2007 at 17:57 UTC ( #655150=perlquestion: print w/replies, xml ) Need Help??

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

I have a list of file names such as the following:
20070911.pdf 20070918.pdf
I'm doing a regex search to pull out the year, month, day and sorting like so:
sort {$b->[3] <=> $a->[3] || $b->[1] <=> $a->[1] || $b->[2] <=> $a->[2 +]} map {$_ =~ m/(\d\d\d\d)(\d\d)(\d\d).pdf/;[$_, $1, $2, $3]} @files
However the sort is choking (not sorting numerically) since it doesn't know how to deal with numbers like 01, 02, etc. Is there anyway in my regex to include just the number suffix if it's prefixed by a 0? (otherwise if it's not prefixed by a 0 include the prefix such as 12). I was messing around with (?<=..) extension, but I wasn't getting the results I needed. Any help would be truly appreciated and I hope my question was clearly defined.

Thanks in advance.
s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;

Replies are listed 'Best First'.
Re: Regex/Sort Questions
by moritz (Cardinal) on Dec 05, 2007 at 18:02 UTC
    However the sort is choking (not sorting numerically) since it doesn't know how to deal with numbers like 01, 02, etc.

    I don't think that is the problem, it should work fine.

    You are sorting by day ($3) first, then by year ($1) and finally by month ($2) - is that really what you want?

    BTW use \. in regexes to match a literal dot .

    Update: fixed description of sort order

      Yes, you are absolutely correct. Thank you for seeing the error in my logic - I knew there was a simple solution (oops).

      s;;5776?12321=10609$d=9409:12100$xx;;s;(\d*);push @_,$1;eg;map{print chr(sqrt($_))."\n"} @_;
Re: Regex/Sort Questions
by ikegami (Patriarch) on Dec 05, 2007 at 18:33 UTC
    Given the format you use, you can simply sort lexically.
    my @oldest_first = sort @file_names;
    my @newest_first = reverse sort @file_names;
Re: Regex/Sort Questions
by KurtSchwind (Chaplain) on Dec 05, 2007 at 19:51 UTC

    ikegami has it spot on.

    The best part of YYYYMMDD is that it Does-The-Right-Thing(tm) automatically in ls (or dir depending on your OS).

    I used to drive a Heisenbergmobile, but every time I looked at the speedometer, I got lost.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2022-08-18 02:37 GMT
Find Nodes?
    Voting Booth?

    No recent polls found