Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Sort files by date in the file name.

by kcott (Archbishop)
on Jul 08, 2016 at 08:43 UTC ( [id://1167446]=note: print w/replies, xml ) Need Help??


in reply to Sort files by date in the file name.

G'day leoberbert,

I think this does what you want:

#!/usr/bin/env perl -l use strict; use warnings; my @files = qw{ 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT }; my @sorted = map { $_->[0] } sort { $a->[1] <=> $b->[1] || $a->[3] <=> $b->[3] } map { [ $_ => split /[_.]/ ] } @files; print join "\n", '@files:', @files; print join "\n", '@sorted:', @sorted;

Output:

@files: 1020300000_XXXXXXXXX_20160707193000.TXT 1020300000_XXXXXXXXX_20160707170000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT @sorted: 1020300000_XXXXXXXXX_20160707170000.TXT 1020300000_XXXXXXXXX_20160707193000.TXT 1020400000_XXXXXXXXX_20160707180000.TXT 1020400000_XXXXXXXXX_20160707190000.TXT

The construct I've used here is known as a Schwartzian Transform.

This sorts first on column1 and then on column3, both in ascending order; this should put your wanted file in $sorted[0]. If that's not what you want, modify the sort { ... } code to suit.

— Ken

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1167446]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (3)
As of 2024-04-24 21:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found