Perl 'Expert' Quiz-of-the-Week #13

Mark Jason Dominus <[email protected]> Wed, 28 May 2003 14:12:29 -0400
Newsgroups gmane.comp.lang.perl.qotw.quiz-of-the-week
Organization Plover Systems
Message-ID <[email protected]>
IMPORTANT: Please do not post solutions, hints, or other spoilers
        until at least 60 hours after the date of this message.
        Thanks.

IMPORTANTE: Por favor, no enviéis soluciones, pistas, o cualquier otra
        cosa que pueda echar a perder la resolución del problema hasta
        que hayan pasado por lo menos 60 horas desde el envío de este
        mensaje. Gracias.

IMPORTANT: S'il vous plaît, attendez au minimum 60 heures après la
        date de ce message avant de poster solutions, indices ou autres
        révélations. Merci.

WICHTIG: Bitte schicken Sie keine Lösungen, Tipps oder Hinweise für
        diese Aufgabe vor Ablauf von 60 Stunden nach dem Datum dieser
        Mail. Danke.

BELANGRIJK: Stuur aub geen oplossingen, hints of andere tips in de
        eerste 60 uur na het verzendingstijdstip van dit
        bericht. Waarvoor dank.

VNIMANIE: Pozhalujsta ne shlite reshenija, nameki na reshenija, i
        voobshe lyubye podskazki v techenie po krajnej mere 60 chasov
        ot daty etogo soobshenija.  Spasibo.

Qing3 Zhu4Yi4: Qing3 Ning2 Deng3Dao4 Jie1Dao4 Ben3 Xin4Xi2 Zhi1Hou4 60
        Xiao3Shi2, Zai4 Fa1Biao3 Jie3Da2, Ti2Shi4, Huo4 Qi2Ta1 Hui4
        Xie4Lou4 Da2An4 De5 Jian4Yi4.  Xie4Xie4.



The 'MH' mail system stores email messages in a 'folder', which is
just a plain directory.  Messages are files in this directory whose
names are numerals.  The directory might contain other files or
subdirectories; these are not messages.

Implement a 'sortby' command that sorts the messages in a folder by
subject.  It should rename the messages in the folder so that (a) the
set of message numbers before and after is the same, and (b)
afterwards, if the subjects were extracted from each message in
message number order, they would be in alphabetic order.  'sortby' is
not allowed to change the contents of the folder in any other way.

'sortby' is invoked like this:

        sortby folder-dir [-f fieldname] [-r]

'folder-dir' is the path to the folder directory.  '-f fieldname', if
present, tells 'sortby' to examine the specified field of each
message, instead of the subject field.  '-r', if present, reverses the
order of the sort.

For example, here's the contents of a folder before sorting:

  393 -11/18 "Barbie"           use strict<<Seeing as much of the Perl communit
  588 -11/17 Maurice Fox        Please unsubscribe me from this group.<<I tried
  590  11/17 Not_a_Number       Subroutines and warnings [SPOILER]<<Oh dear. Ha
  605  11/07 Adam Lopresto      Expert Quiz 4 Data<<Does anyone have some test 

Here it is after sorting:

  393  11/07 Adam Lopresto      Expert Quiz 4 Data<<Does anyone have some test 
  588 -11/17 Maurice Fox        Please unsubscribe me from this group.<<I tried
  590  11/17 Not_a_Number       Subroutines and warnings [SPOILER]<<Oh dear. Ha
  605 -11/18 "Barbie"           use strict<<Seeing as much of the Perl communit

Here it is after 'sortby -f From':

  393  11/07 Adam Lopresto      Expert Quiz 4 Data<<Does anyone have some test 
  588 -11/18 "Barbie"           use strict<<Seeing as much of the Perl communit
  590 -11/17 Maurice Fox        Please unsubscribe me from this group.<<I tried
  605  11/17 Not_a_Number       Subroutines and warnings [SPOILER]<<Oh dear. Ha

I won't be sending a postmortem on Monday, because I will be in
Hawai`i.  (See http://www.geekcruises.com/home/pw3_home.html ) I will
send one when I get back, probaly on June 16.