Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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

— Ken


In reply to Re: Challenge: sort weekdays in week-order (elegantly and efficiently) by kcott
in thread Challenge: sort weekdays in week-order (elegantly and efficiently) by bliako

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

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

    No recent polls found