Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re^2: Formatting dates while parsing CSV

by BigRedEO (Acolyte)
on Jun 14, 2016 at 14:35 UTC ( [id://1165590]=note: print w/replies, xml ) Need Help??


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

I thought the formatting would do the re-ordering for me. However, having just tried this line, it's now telling me "Missing argument in sprintf..."?
  • Comment on Re^2: Formatting dates while parsing CSV

Replies are listed 'Best First'.
Re^3: Formatting dates while parsing CSV
by Corion (Patriarch) on Jun 14, 2016 at 14:44 UTC

    I can't replicate your problem with the above source code and Perl 5.14.

    If you get this problem with a different program, or different input, you will have to show us that program and that input.

    For example, I can provoke that error message by removing an index from the slice after split:

    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; my @fields = 'a' .. 'z'; @fields[ 10, 14, 24, 26] = ('1/2/2016') x 4; # My change is below $_ = sprintf '%04d-%02d-%02d', (split m:/:)[ 0, 1 ] for @fields[ 10, 1 +4, 24, 26 ]; say join ',', @fields; __END__ Missing argument in sprintf at tmp.pl line 8. Missing argument in sprintf at tmp.pl line 8. Missing argument in sprintf at tmp.pl line 8. Missing argument in sprintf at tmp.pl line 8. a,b,c,d,e,f,g,h,i,j,0001-02-00,l,m,n,0001-02-00,p,q,r,s,t,u,v,w,x,0001 +-02-00,z,0001-02-00

    So, a likely issue is that you did not use the exact code that choroba posted.

      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?
        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,
        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,

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1165590]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found