G'day bliako,
Here's another way:
enum for the order; and, an Orcish Manoeuvre for efficiency.
#!/usr/bin/env perl
use 5.010;
use strict;
use warnings;
use enum qw{sun mon tue wed thu fri sat};
say '*** Check Orcish Manoeuvre is working ***';
say _canon_day_test($_) for qw{Mon mon Monday monday} x2;
say '*** Your sort example ***';
my @weekdays = qw/Monday Saturday Thursday/;
say for sort { _canon_day($a) <=> _canon_day($b) } @weekdays;
say '*** More complex example ***';
my @many_form_weekdays = qw{
thurs tue Thu Tues Thursday tuesday
Fri Sun sat Fri Sun sat SATURDAY FRIDAY SUNDAY
Wednesday Mon wed monday
sun Sat friday Friday Sunday
};
say for sort { _canon_day($a) <=> _canon_day($b) } @many_form_weekdays
+;
sub _canon_day {
my ($day) = @_;
state $day_for = {};
return $day_for->{$day} ||= eval lc substr $day, 0, 3;
}
sub _canon_day_test {
my ($day) = @_;
state $day_for = {};
say "'$day' needs Orc" unless exists $day_for->{$day};
return $day_for->{$day} ||= eval lc substr $day, 0, 3;
}
_canon_day_test() is identical to _canon_day() except for the say statement:
it's just for testing.
You may want to add some validation code, but that wasn't part of your question.
Output:
*** Check Orcish Manoeuvre is working ***
'Mon' needs Orc
1
'mon' needs Orc
1
'Monday' needs Orc
1
'monday' needs Orc
1
1
1
1
1
*** Your sort example ***
Monday
Thursday
Saturday
*** More complex example ***
Sun
Sun
SUNDAY
sun
Sunday
Mon
monday
tue
Tues
tuesday
Wednesday
wed
thurs
Thu
Thursday
Fri
Fri
FRIDAY
friday
Friday
sat
sat
SATURDAY
Sat
-
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 How to display code and escape characters
are good places to start.
|