Here's a little problem, for which I can think up some longish solutions, but not, from the top of my head, a simple, elegant, thus "perlish" solution. So, I'm inviting you to come up with one. It could even be a candidate for Perl Golf, though personally I'd prefer a simple and elegant solution to a golfed obfuscated one. Anyway...

I'm parsing a database description for a Progress database, and I want to create an importer for Oracle for an exported flatfile. One of the fields is of the type "date". Now, at first sight, I find format specifications for dates like "99/99/9999", which should produce a format "DD/MM/YYYY", and "99/99/99", producing "DD/MM/RR", for use in a to_date() call. Note: "RR" specifies a 2 digit year, which will be considered in the range 2000-2049 if it's < 50, and between 1950 and 1999 if >= 50.

Simple enough, a conversion hash

%dateformat = ( '99/99/99' => 'DD/MM/RR', '99/99/9999' => 'DD/MM/YYYY', '9999/99/99' => 'YYYY/MM/DD' );
would do... Except: I want to be able to accept other separators than slashes, too. The script should be smart enough to produce the appropriate format with the actual separators. In short, it should produce 'DD-MM-YYYY' out of '99-99-9999', for example.

Your assignment is to write some Perl code which does this conversion. A generic solution, using a data table for the production, is preferred over a hardwired solution, using a code block for each pattern.

I'll post a solution of my own in a follow-up, later.


In reply to Generating a format template for a date by bart

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.