in reply to Re^2: sorting dates in YYYYMMDD format
in thread sorting dates in YYYYMMDD format
..I solved the problem..
Good... but why not just use sort just as you have been told previously on this post.
Yes your regexes matched successfully, see
...produces...use re 'debug'; @dates = ('20130601', '20130401', '20130501'); my @sorted = map $_->[1], sort { $a->[1] <=> $b->[1] } map [ $_, join('', (/(..)(..)(....)/)[0,1,2]) ], @dates; # DD-MM-YYYY print "@sorted\n";
Compiling REx `(..)(..)(....)' size 21 Got 172 bytes for offset annotations. first at 3 1: OPEN1(3) 3: REG_ANY(4) 4: REG_ANY(5) 5: CLOSE1(7) 7: OPEN2(9) 9: REG_ANY(10) 10: REG_ANY(11) 11: CLOSE2(13) 13: OPEN3(15) 15: REG_ANY(16) 16: REG_ANY(17) 17: REG_ANY(18) 18: REG_ANY(19) 19: CLOSE3(21) 21: END(0) minlen 8 Offsets: [21] 1[1] 0[0] 2[1] 3[1] 4[1] 0[0] 5[1] 0[0] 6[1] 7[1] 8[1] 0[0] 9[1] 0 +[0] 10[1] 11[1] 12[1] 13[1] 14[1] 0[0] 15[0] Matching REx `(..)(..)(....)' against `20130601' Setting an EVAL scope, savestack=12 0 <> <20130601> | 1: OPEN1 0 <> <20130601> | 3: REG_ANY 1 <2> <0130601> | 4: REG_ANY 2 <20> <130601> | 5: CLOSE1 2 <20> <130601> | 7: OPEN2 2 <20> <130601> | 9: REG_ANY 3 <201> <30601> | 10: REG_ANY 4 <2013> <0601> | 11: CLOSE2 4 <2013> <0601> | 13: OPEN3 4 <2013> <0601> | 15: REG_ANY 5 <20130> <601> | 16: REG_ANY 6 <201306> <01> | 17: REG_ANY 7 <2013060> <1> | 18: REG_ANY 8 <20130601> <> | 19: CLOSE3 8 <20130601> <> | 21: END Match successful! Matching REx `(..)(..)(....)' against `20130401' Setting an EVAL scope, savestack=12 0 <> <20130401> | 1: OPEN1 0 <> <20130401> | 3: REG_ANY 1 <2> <0130401> | 4: REG_ANY 2 <20> <130401> | 5: CLOSE1 2 <20> <130401> | 7: OPEN2 2 <20> <130401> | 9: REG_ANY 3 <201> <30401> | 10: REG_ANY 4 <2013> <0401> | 11: CLOSE2 4 <2013> <0401> | 13: OPEN3 4 <2013> <0401> | 15: REG_ANY 5 <20130> <401> | 16: REG_ANY 6 <201304> <01> | 17: REG_ANY 7 <2013040> <1> | 18: REG_ANY 8 <20130401> <> | 19: CLOSE3 8 <20130401> <> | 21: END Match successful! Matching REx `(..)(..)(....)' against `20130501' Setting an EVAL scope, savestack=12 0 <> <20130501> | 1: OPEN1 0 <> <20130501> | 3: REG_ANY 1 <2> <0130501> | 4: REG_ANY 2 <20> <130501> | 5: CLOSE1 2 <20> <130501> | 7: OPEN2 2 <20> <130501> | 9: REG_ANY 3 <201> <30501> | 10: REG_ANY 4 <2013> <0501> | 11: CLOSE2 4 <2013> <0501> | 13: OPEN3 4 <2013> <0501> | 15: REG_ANY 5 <20130> <501> | 16: REG_ANY 6 <201305> <01> | 17: REG_ANY 7 <2013050> <1> | 18: REG_ANY 8 <20130501> <> | 19: CLOSE3 8 <20130501> <> | 21: END Match successful! 20130401 20130501 20130601 Freeing REx: `"(..)(..)(....)"'
However, if you must use "Schwartzian transform", you can sort on the month, since the year is the same like this:
But sincerely, for this use sort just like other monks told you.use warnings; use strict; my @dates = ('20130601', '20130401', '20130501'); print join ' ' => map{$_->[0]} sort{$a->[1] <=> $b->[1]} map{/.{4}(.{2})/;[$_,$1]} @dates;
If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me
In Section
Seekers of Perl Wisdom