954,523 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Help needed with CSV file, how to print 2nd word from 2nd line

Hello, I was wondering if anyone could help me with something.

I am not a programmer yet I am able to modify scripts to a certain extent. I recently downloaded a script to read an existing CSV file and output via an SSI call. What I would like to do is to be able to read the third entry from the 2nd row, for example:

Bob;565 Apple Tree Lane;Vancouver;Canada
Fred;1234 Maple Road;Montreal;Canada

I would need to get/print the word "Montreal" (third entry from the second line, or any of my choice) from the existing CSV file and output using SSI, but not in a table, just as plain text with no carriage return, can you help?

Here is the CGI script (via SSI) I am presently using, yet it is printing the third entry from both lines. I need to extract/print the 3rd entry from the 2nd line only:

#!/usr/bin/perl
#
#

use CGI::Carp qw(fatalsToBrowser);
use strict;

my $csvfile = "/home/public_html/cgi-bin/database/mycsvfile.csv";

print "Content-Type: text/html\n\n";

open (CSV, "< $csvfile");
while(){
my $cols = (split(/;/))[3];
print $cols,"\n";
}
close CSV

fredfletcher
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 
open (FIN, "test.csv") || die "Cannot Open the input file";
@file=<FIN>;
close (FIN);

## CSV files are separated by comma
## So, split the data by using of 'comma' 
@get=split/,/, $file[1];
print "\nSecond word from second line : ", $get[1];
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

Thanks for the help, but am getting errors.

Here is my re-write:

#!/usr/bin/perl
#
#

use CGI::Carp qw(fatalsToBrowser);
use strict;

print "Content-Type: text/html\n\n";

open (FIN, "/home/public_html/cgi-bin/database/myfile.csv") || die "Cannot Open the input file";
@file=;
close (FIN);

## CSV files are separated by comma
## So, split the data by using of 'comma'
@get=split/;/, $file[1];
print "\nSecond word from second line : ", $get[1];

and these are the errors:

Software error:

Global symbol "@file" requires explicit package name at ./test_0.cgi line 11.
Global symbol "@get" requires explicit package name at ./test_0.cgi line 16.
Global symbol "@file" requires explicit package name at ./test_0.cgi line 16.
Global symbol "@get" requires explicit package name at ./test_0.cgi line 17.
Execution of ./test_0.cgi aborted due to compilation errors.

Note: I use a semi-colon in the CSV instead of a comma, so I changed @get=split/,/, $file[1]; to @get=split/;/, $file[1];

Any idea why I'm getting errors?

Thank you :)

fredfletcher
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 


place 'my' infront the variable namee.

my @file=<FIN>;
my @get=split/;/, $file[1];
k_manimuthu
Junior Poster in Training
93 posts since Jun 2009
Reputation Points: 55
Solved Threads: 24
 

you need to use "my" when you first declare a variable and use strict :)

EDIT: oh nvm i was too late^^"

replic
Light Poster
47 posts since Nov 2008
Reputation Points: 27
Solved Threads: 6
 

Thanks to everyone for the info :) I appreciate it, even though you were a bit late there "Replic" :)

Have a great day!

oh, and it works beautifully, thanks again for the codes!

fredfletcher
Light Poster
34 posts since Jan 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: