Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Formatting dates while parsing CSV

by choroba (Cardinal)
on Jun 14, 2016 at 14:27 UTC ( [id://1165588]=note: print w/replies, xml ) Need Help??


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

You were almost there. Why did you remove the reordering of the date parts?
$_ = sprintf '%04d-%02d-%02d', (split m:/:)[2, 0, 1] for @fields[10, 1 +4, 24, 26];

Update: Tested with
#!/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; $_ = sprintf '%04d-%02d-%02d', (split m:/:)[ 2, 0, 1 ] for @fields[ 10 +, 14, 24, 26 ]; say join ',', @fields;

($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,

Replies are listed 'Best First'.
Re^2: Formatting dates while parsing CSV
by BigRedEO (Acolyte) on Jun 14, 2016 at 14:35 UTC
    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..."?

      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?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (10)
As of 2024-04-18 09:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found