mkmcconn has asked for the wisdom of the Perl Monks concerning the following question:
I was given the following problem at work. A DATA sample and my solution follows the READMORE tag.
Problem: Print a directory of engineering drawings in the order determined by drawing number data contained in the filename:
TYPE Part (Detail) Revision (ALT) layer 1D 1 R1 B1 .vec 18D 20 A R0 .ras
Sort order:
- layer,
- reverse(ALT),
- TYPE,
- Part,
- Revision,
- Detail
ALT, if it exists, must be queued in reverse order (e.g. 3,2,1,'')
- How would you have done it?
- How does your solution scale better or work more reliably?
- How would you make the solution easier to understand and maintain?
#!/usr/bin/perl -wl use strict; for (sort{ # TYPE: $t0_$t1_ # Part: $p0_ # Det : $pda # Rev : $r_ # ALT : $alt_ # Layer $l_ my ($t0a,$t1a,$p0a,$pda,$ra,$alta,$la) = map(m!(\d+)(\D+)(\d+)(\D*)R(\d+)B?(\d*)\.(\w+)$!,$a); my ($t0b,$t1b,$p0b,$pdb,$rb,$altb,$lb) = map(m!(\d+)(\D+)(\d+)(\D*)R(\d+)B?(\d*)\.(\w+)$!,$b); $la cmp $lb || ($altb eq ''? 0 :$altb) <=> ($alta eq '' ? 0 : $alta) || $t0a <=> $t0b || $t1a cmp $t1b || $p0a <=> $p0b || $ra cmp $rb || $pda cmp $pdb; }<DATA>){ chomp; print; } __DATA__ 18DD13AR0B2.vec 5A344R2B15.ras 5A344R2B15.vec 5A35SR0.ras 113A28R1.vec 113A28AR0.vec 113A29R0.ras 5A32R0.ras 113A29R1.vec 18AD13XR0B3.ras 18AD13XR0B3.vec 113A22R1.ras 5A35R0.vec 5A34R0.ras 113A22R1.vec 18DD12YR0B2.vec 18DD13AR0B2.ras 113A29AR0.ras 113A29AR2.vec 113A28R0.vec 5A33R0.vec 5A34R0.vec 5A35R0.ras 113A28AR2.vec 113B28R2.ras 113A28R2.vec 13B29AR0B1.ras 113B29AR0B1.vec 5A32R0.vec 5A33R0.ras
Explanation:
|
---|
Replies are listed 'Best First'. | |
---|---|
(Ovid) Re: complex sort
by Ovid (Cardinal) on Dec 25, 2001 at 03:58 UTC | |
Re: complex sort
by Juerd (Abbot) on Dec 25, 2001 at 03:06 UTC | |
by jynx (Priest) on Dec 25, 2001 at 04:51 UTC | |
Re: complex sort
by grinder (Bishop) on Dec 25, 2001 at 03:09 UTC | |
by Juerd (Abbot) on Dec 25, 2001 at 03:15 UTC | |
Re: complex sort
by mkmcconn (Chaplain) on Dec 25, 2001 at 06:16 UTC | |
by Anonymous Monk on Dec 25, 2001 at 06:52 UTC | |
Re: complex sort
by George_Sherston (Vicar) on Dec 25, 2001 at 03:34 UTC |
Back to
Seekers of Perl Wisdom