in reply to Re^3: Formatting dates while parsing CSV
in thread Add leading zeros to days/months in dates while parsing CSV

Wait - I may have figured it out. Later in the code, if those same four fields are empty, I'm simply substituting it with "0000-00-00". I'm guessing that my original "join" statement doesn't care if that field is empty, but sprintf does?
  • Comment on Re^4: Formatting dates while parsing CSV

Replies are listed 'Best First'.
Re^5: Formatting dates while parsing CSV
by choroba (Cardinal) on Jun 14, 2016 at 15:35 UTC
    The problem is that the array slice doesn't return undefs when slicing an empty list, it just doesn't return anything:
    $ perl -wE 'say for ()[2, 1, 0]' $ perl -wE 'say for ("a")[2, 1, 0]' Use of uninitialized value in say at -e line 1. Use of uninitialized value in say at -e line 1. a

    Easy fix: supply the missing fields:

    $_ = sprintf '%04d-%02d-%02d', ((split m:/:), (0) x 3)[ 2, 0, 1 ] for +@fields[ 10, 14, 24, 26 ];

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      So just adding the "(0) x3" will add zeros to any empty date fields, but format if there is an actual date?
        it appends a list of three elements (each containing a zero). So if the split returns a list of three, you end up with six elements (where you take the first three and ignore the rest); if there's nothing to split, you end up with the three freshly appended zeroes.

        Probably exactly what you want.

Re^5: Formatting dates while parsing CSV
by choroba (Cardinal) on Jun 14, 2016 at 15:25 UTC
    Arrays are indexed from 0 in Perl:
    0 1 2 3 4 5 6 7 +8 9 10 11 12 13 640399762,1,1,colorant ran out during shot,20,IFC 8112NP,08DX8020212,, +1,1/31/2016,181315,A82T00154,7674,1/30/2016
    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
      Yes - but my code is adding a concatenated field to the beginning of each record first.