Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

help me parse a csv file

Status
Not open for further replies.

tmh

Technical User
Mar 6, 2001
8
US
I am a beginner at Perl, but am working on a team to import a csv file into an Access Database as a table. My part is really to write the SQL code which I am familiar with, but our team can't get the data from the csv file so I can manipulate it with SQL. We researched how to bring the csv file into an array and assign each point of the array a variable name so it can be written to the Access database using SQL. We have been using a sample code that is not working. Can anyone tell us either why it won't work or give us an example of what we can do. I have included the code followed by the errors we are getting.

Here is the code we are testing:
---------------------------------------------------

require Text::CSV;
my $csv = Text::csv->new;

my $column = '';
my $sample_input_string = '"I said, ""Hi!""",Yes,"",2.34,,"1.09"';
if ($csv->parse($sample_input_string)) {
my @field = $csv->fields;
my $count = 0;
for $column (@field) {
print ++$count, " => ", $column, "\n";
}
print "\n";
} else {
my $err = $csv->error_input;
print "parse() failed on argument: ", $err, "\n";
}


my @sample_input_fields = ('You said, "Hello!"',
5.67,
'Surely',
'',
'3.14159');
if ($csv->combine(@sample_input_fields)) {
my $string = $csv->string;
print $string, "\n";
} else {
my $err = $csv->error_input;
print "combine() failed on argument: ", $err, "\n";
}


--------------------------------------------


We are getting the following errors

Can't locate auto/text/csv/autosplit.ix in @inc (@inc contains: c:/perl/lib c:/perl/file/lib .)
at c:/perl/lib/autoloader.pm line 127
at c:/perl/lib/text/csv.pm line 23

can't locate object method "new" via package
"text::csv" at test1.pl line 4

------------------------
My script is called test1.pl and I am running it via a bat file.

Thanks for reading this in an attempt to assist!!!!
tmh
 
You need to add the library to your @INC. do this:

use lib wherever_the_Lib_is

at the begining of the script Hope that gets it


Miah
 
I don't understand how to do that. I am just a beginner. I have continued to research this and I think one of my problems is that I don't have the Text::CSV module installed. I found it on CSPAN, but can't figure out how to properly install it. I am running a Win32 system. I would like to be able to use the quick install that is offered on CSPAN, but I can't seem to make it work. Any assistance is greatly appreciated. Thanks.

Michelle
 
Okay I don't use Win32, I use Solaris, but let me see here there are a few basics. Know where you want to put it I put them in /opt/local/lib/perl5/5.00502, so I would look for where your win32 perl is and then the modules should be around there, and put it there. Unzip it and tar it, if you do not have the correct tools checkout
Then use the README/INSTALL. it should tell you to do

perl Makefile.PL
make
make test
make install

That should be it. It is a little overwhelming but it is easy to install modules

Miah
 
Thanks Miah.

I had to download nmake.exe and nmake.err from microsoft's ftp server. It came in the form of nmake15.exe. After I did that, the nmake works the same as the make. I put the nmake.exe and nmake.err in my path and then downloaded the modules I wanted. I put the module in a seperate temp folder and extracted the files from the tr file. After that, I went to the temp directory from my DOS shell and ran the commands you recommended above. Only I used nmake in place of make.

Thanks for pointing me in the right direction.

Michelle

I finally figured it out.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top