I have a Perl script to scrub data in CSV files to then be loaded into MySQL Tables. Several of the fields are Date fields that were originally m/d/yyyy - with no leading zeros. I formatted those fields for MySQL using -

$_ = join '-', (split /\//)[2,0,1] for @fields[10,14,24,26];

which worked just fine to load the MySQL tables. But now I'm being told these same CSV files may be used in other depts for things like SQL or other programs and they will need leading zeros. I tried using sprintf, but I'm not quite sure how to use it while parsing nor with a scalar variable. This is what I attempted in place of my original line of code, but get "unitialized value" -

$_ = sprintf '%04d-%02d-%02d', split m:/: for @fields[10,14,24,26];

What am I doing wrong here? (sorry, parsing and scalars get me confused)


In reply to Add leading zeros to days/months in dates while parsing CSV by BigRedEO

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.