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
#
# <!--#exec cgi="my_ssi_file.cgi" -->

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(<CSV>){
my $cols = (split(/;/))[3];
print $cols,"\n";
}
close CSV

Recommended Answers

All 5 Replies

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];

Thanks for the help, but am getting errors.

Here is my re-write:

#!/usr/bin/perl
#
# <!--#exec cgi="my_ssi_file.cgi" -->

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=<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];

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 :)

place 'my' infront the variable namee.

my @file=<FIN>;
my @get=split/;/, $file[1];

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

EDIT: oh nvm i was too late^^"

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!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.