Welcome to WebmasterWorld Guest from

Forum Moderators: coopster & jatar k & phranque

Message Too Old, No Replies

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

I need to print/extract the 2nd entry from the 2nd line in a CSV file



7:44 pm on Jan 4, 2011 (gmt 0)

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 have 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 3rd column/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 an 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. It works, yet it is printing the third entry from both lines. I need to extract/print the 3rd entry from the 2nd line only:

(may need to be completely rewritten)

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

[edited by: phranque at 9:36 am (utc) on Jan 5, 2011]
[edit reason] disable graphic smile faces [/edit]


8:40 pm on Jan 4, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

That sound simple enough.

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

open (CSV, "<", $csvfile);
my @lines = <CSV>;
my $cols = (split(/;/, $lines[2]))[2];
print $cols,"\n";
close CSV;

$lines[2] in the third line selects the row. starts at 0.


9:24 pm on Jan 4, 2011 (gmt 0)

10+ Year Member

I would change the $cols to an array, so you'd have access to all of the elements:

my @cols = split(/;/, $_);

Then, to grab only the second (or whatever) line, you could add an if statement:

if (2..2) {print $cols[2],"\n";}

Thus, the while block becomes:

while( <CSV> )
my @cols = split(/;/, $_);
if (2..2) {print $cols[2],"\n";}

If you need to print different lines, simply change the "2..2" to "1..3" (lines one to three), or "3..3" (line three), or whatever.

If you need to print more than one element of @cols, you can change $cols[2]:

print "$cols[0] lives in $cols[2], $cols[3] with the address of $cols[1].\n";

(You'd need to "chomp $cols[3];" to get rid of the newline char, if you want it all on one line.)


9:40 pm on Jan 4, 2011 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member

if (2..2) {print $cols[2],"\n";}

that looks like very, very black magic but it actually works. I like it. I've only used ".." in for-loops, but perlop just opened my eyes to many other options.
Great idea (although I still think 2..2 look evil)!


9:56 pm on Jan 4, 2011 (gmt 0)

10+ Year Member

Oh, it is evil! It's perl! ;-)

See page 103-104 of the Camel book.


10:17 pm on Jan 4, 2011 (gmt 0)

You guys are just simply the best! Thank you so much, I really appreciate this!

I've tried them all and they work very well; I will be using them both.

Again, a big THANK YOU :) and a Happy New Year!


3:52 am on Jan 7, 2011 (gmt 0)

Hi again,

I have another question:

Is there a way to implement the time and/or date after ($cols[1]) for example.

And if so, would I be able to modify the date/time format?

The reason I ask for the different formats, is that I need a way to show the month in our both official languages in Canada, English and french.

I tried an SSI call <!--#exec cgi="/cgi-bin/hours_e.cgi" --> but that didn't work. Tried replacing the " with ' and that didn't work.



4:19 am on Jan 7, 2011 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

welcome to WebmasterWorld, Fred!

these references would be a good place to start.
http://perldoc.perl.org/perllocale.html [perldoc.perl.org]

http://perldoc.perl.org/functions/localtime.html [perldoc.perl.org]


http://search.cpan.org/~drolsky/DateTime-0.66/lib/DateTime.pm [search.cpan.org]


4:56 pm on Jan 7, 2011 (gmt 0)

Thank you, it helped :)

Featured Threads

Hot Threads This Week

Hot Threads This Month