Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re: Unpack or substr to create CSV?

by hdb (Monsignor)
on May 02, 2015 at 16:16 UTC ( [id://1125453]=note: print w/replies, xml ) Need Help??


in reply to Unpack or substr to create CSV?

I only count 43 per line, could this be a source of your problems? There are many ways to do this, for example using regular expressions:

use strict; use warnings; while(<DATA>){ s/(.)(.{15})(.{14})(.{13})/$1,$2,$3,$4/; print; } __DATA__ 0123456789012345678901234567890123456789012 C4432882490H019000020150211ESL6690 0H2015PC C4833076550HC0P0000201412093J46651 0H2015DX C6033106980H057130020150323FRE7602 0H2015PC C663160140MT007015G20141124274847A MT2015PC

Replies are listed 'Best First'.
Re^2: Unpack or substr to create CSV?
by AnomalousMonk (Archbishop) on May 02, 2015 at 17:17 UTC

    Not to mention that it's really easy to add data validation with this approach:

    use strict; use warnings; while(<DATA>){ s/\A(.)(.{15})(.{14})(.{13})\Z/$1,$2,$3,$4/ or die "bad record: '$_'"; print; } __DATA__ 0123456789012345678901234567890123456789012 C4432882490H019000020150211ESL6690 0H2015PC C4833076550HC0P0000201412093J46651 0H2015DX C6033106980H057130020150323FRE7602 0H2015PC C663160140MT007015G20141124274847A MT2015PC

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (3)
As of 2024-04-25 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found