Code Comments

Programming Forum and web based access to our favorite programming groups.
For Programmers: Free Programming Magazines | New: Database administration forum
Registration is free! Edit your profileCalendarFind other membersFrequently Asked QuestionsSearch -> 
Post New Thread











Thread
Author

reading in a CVS file
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

Report this thread to moderator Post Follow-up to this message
Old Post
David Gilden
11-02-04 08:55 PM


Re: reading in a CVS file
> 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,
$emai
l);
>
> ## 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


Report this thread to moderator Post Follow-up to this message
Old Post
Wiggins d Anconia
11-02-04 08:55 PM


Sponsored Links




Last Thread Next Thread Next
Search this forum -> 
Post New Thread

PERL CGI Beginners archive

Show a Printable Version Send to friend Email This Page to Someone! subscribe to this thread Receive updates to this thread
Computer Consultants
Programming Jobs
Visual Basic Controls
SQL Server Programming
Webservices
Java Security
Visual Studio
C# Programming
Visual J++
Software engineering
Open source Software
Perl Programming
PHP Programming
ASP Programming
ASP .NET Programming
Visual Basic Programming
Windows Scripting Host
Java Programming
Java Help
Java Beans
VBScript
Cobol
MAC Applications
Unix Programming
Forum Jump:
All times are GMT. The time now is 05:44 AM.

 
Free MCSE Braindumps | Real Estate Topics

Programming forum archive

Copyrights CodeComments.com 2004 - 2006

Powered by vBulletin Copyright 2000-2006 Jelsoft Enterprises Limited.