CSV_XS: removing leading '0' from a parsed field

A

Alex Hunsley

I'm using CSV_XS to parse a CSV file.
One of my CSV fields beings with the 0 char as it is a phone number, e.g.:

"0123456789"

However, the string returned from CSV_XS when I parse the line doesn't
have the leading 0. I think this may be because it is intrepretting it
as a number at some point, and hence dropping the leading 0?
Anyway, my code for reading my file is as follows:

use DBI;

use DBD::mysql;

use Text::CSV_XS;
use File::Find;

#$vah = "07";
#print $vah;
#exit;

$csv = Text::CSV_XS->new(
{
'quote_char' => '"',
'escape_char' => '"',
'sep_char' => ',',
'binary' => 0,
'always_quote' => 1
}
);

open(INPUT,"<sheet.csv") || die "couldn't open input";


while (<INPUT>) {
$inputLine = $_;

#skip blank lines
if ( $inputLine =~ /^\s*$/ ) {
next;
}


# parse a CSV string into fields
$readStatus = $csv->parse($inputLine);
@columns = $csv->fields(); # get the parsed fields

# this folllowing line output lines like 1234
# rather than the expected 01234!

print "$columns[9]\n";


}

close(INPUT);


grateful for any guidance here...
thanks
alex
 
A

Alex Hunsley

Alex said:
I'm using CSV_XS to parse a CSV file.
One of my CSV fields beings with the 0 char as it is a phone number, e.g.:

"0123456789"

However, the string returned from CSV_XS when I parse the line doesn't
have the leading 0. I think this may be because it is intrepretting it
as a number at some point, and hence dropping the leading 0?
Anyway, my code for reading my file is as follows:

use DBI;

d'oh! Please ignore last post.
Turns out I managed to remove the 0 myself by saving the spreadsheet
without the column set to the right type. Nothing to do with CSV_XS or perl.

alex
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top