For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > November 2004 > reading in a CVS file









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author reading in a CVS file
David Gilden

2004-11-02, 3:55 pm

Hello,

I am trying to do an import of a Tab De-limited file and then=20
extract certain fields.

I am looking to general comments and or a better more concise way of doing =
this,
Thanks in advance...

Dave Gilden -- Ft. Worth-less Texass

Here is what I have so far (and it is not tested and my be incorrect!)


###Perl Code###

open (FH, $fileIn) || "die Problem reading $fileIn $!\n";

while(<FH> ){
push(@musicians, $_);=20
}=20
close FH;

=20
foreach (@musicians){
chomp;

@tmpArray =3D split(/\t/,$_);=20

$ssNum =3D $tmpArray[0];
$fName =3D $tmpArray[3];
$lName =3D $tmpArray[4];
$zip =3D $tmpArray[8];
$city =3D $tmpArray[9];
$state =3D $tmpArray[10];
$memberStatus =3D $tmpArray[12];
$phone =3D $tmpArray[19];
$instrument =3D $tmpArray[21];
$email =3D $tmpArray[33];


#### INSERT Into Data in to mySql Database ###################

# using place holders
$sql =3D "insert into $table_name values (null, ?, ?, ?, ?,?,?,?, ?, ?, ?);=
";
$sth =3D $dbh->prepare($sql);
$sth-> execute($ssNum,$fName,$lName,$zip,$city,
$state,$memberStatus,$phone,$=
instrument,$email);

## more code.....
}




Visit my schedule page for up to the minute performance info:
<http://www.coraconnection.com/cgi-bin/schedule.pl>


=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D
Cora Connection: Your West African Music Source
Resources, Recordings, Instruments & More!
<http://www.coraconnection.com/>=20
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
3D=3D=3D=3D=3D=3D=3D=3D
Wiggins d Anconia

2004-11-02, 3:55 pm

> Hello,
>
> I am trying to do an import of a Tab De-limited file and then
> extract certain fields.
>


This has what to do with CGI? Your question is better asked to
beginners@perl.org....

> I am looking to general comments and or a better more concise way of

doing this,
> Thanks in advance...
>
> Dave Gilden -- Ft. Worth-less Texass
>
> Here is what I have so far (and it is not tested and my be incorrect!)
>
>
> ###Perl Code###
>


use strict;
use warnings;

> open (FH, $fileIn) || "die Problem reading $fileIn $!\n";
>


I suspect the above ought to have C<die> either outside the string, or
in addition to the string. Have you tested this code?

> while(<FH> ){
> push(@musicians, $_);
> }
> close FH;
>
>
> foreach (@musicians){
> chomp;
>
> @tmpArray = split(/\t/,$_);
>
> $ssNum = $tmpArray[0];
> $fName = $tmpArray[3];
> $lName = $tmpArray[4];
> $zip = $tmpArray[8];
> $city = $tmpArray[9];
> $state = $tmpArray[10];
> $memberStatus = $tmpArray[12];
> $phone = $tmpArray[19];
> $instrument = $tmpArray[21];
> $email = $tmpArray[33];
>


Why bother transferring the individual array values into named variables
if you don't intend on using the names anywhere. What happens when the
columns are in a different order?

>
> #### INSERT Into Data in to mySql Database ###################
>
> # using place holders
> $sql = "insert into $table_name values (null, ?, ?, ?, ?,?,?,?, ?, ?,

?);";
> $sth = $dbh->prepare($sql);
>

$sth-> execute($ssNum,$fName,$lName,$zip,$city,
$state,$memberStatus,$phone,$instrument,
$email);
>
> ## more code.....
> }
>


MySQL comes with an import utility that I believe will take CSV/TSV
files. Alternatively you may want to check out the Text::CSV module, or
one of the other offshoots. WHat happens when one of the fields
contains a tab, aka it is quoted?

http://danconia.org

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com