Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: CSV File Parsing

by Generoso (Vicar)
on Mar 01, 2013 at 02:54 UTC ( #1021142=note: print w/ replies, xml ) Need Help??


in reply to CSV File Parsing

Maybe this is what you are taring to do.

#!/usr/bin/perl # use strict; use warnings; use Text::CSV_XS; my $csv = Text::CSV_XS->new(); #open (CSV, "<", $file_to_parse) or die "Could not open '$file' $!<br> +"; while (my $line = <DATA>) { chomp $line; if ($csv->parse($line)) { my @columns = $csv->fields(); $columns[0] =~ s/ //g; $columns[2] =~ s/ //g; if( ($columns[0]=~/^[0-9]+$/) && ($columns[2]=~/^[0-9]+$/) ) { print "Format ok\n"; foreach (@columns){print $_,"\t";}; print "\n"; }else{ print "Wrong Format\n"; print "First $columns[0] and tird $columns[2] have to be n +umeric\n"; } }else{ my $err = $csv->error_input; print "<br><b>$err Failed to parse line, please call for h +elp!</b><br>" if $err; } } __DATA__ 12345,John Dow, 123456 54321,ANDREW MALL, 8888882 987865,BOGGS BUNNY, 2234566 88777,CLARK KENT R, 888666 126677,CLARK KENT R, xx345778 112222,DAVOLIO DARK F, 3321777

Results

perl "E:\perl_TK\perlCSV.pl" Process started >>> Format ok 12345 John Dow 123456 Format ok 54321 ANDREW MALL 8888882 Format ok 987865 BOGGS BUNNY 2234566 Format ok 88777 CLARK KENT R 888666 Wrong Format First 126677 and tird xx345778 have to be numeric Format ok 112222 DAVOLIO DARK F 3321777 <<< Process finished.


Comment on Re: CSV File Parsing
Select or Download Code

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (4)
As of 2014-09-22 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls