PDA

View Full Version : CSV files, how do i use them?


D2K2
11-12-2002, 09:57 PM
Hi, how do i use a CSV file? ATM i'm only getting the last value from the lot...

File:
thing1,0,thing2,1

CGI (Only the Split bit, the rest SHOULD work...):
open (LINKS, "nonactive/pages/$sela/$selb/$selc.d2k2page") || die "Failed to open nonactive/pages/$sela/$selb/$selc.d2k2page - $!\n";
my @page = <LINKS>;
close (LINKS);
my @pag;
if ($selc eq "index"){
@pag = split (/,/, @page);
}

Output:
1

Thanks in advance for any help :)

Mouldy_Goat
11-14-2002, 12:28 AM
Hi D2K2,

I think the problem may be that you're trying to use split on an array. What you want to do is assign the contents of the file to a scalar variable, and then use the split on this.

This is presuming that the file contains just:
thing1,0,thing2,1

and that this isn't the line format for a file which contains more than one line. Hope that made sense..

Also it's never a bad idea to chomp() something like this.

So what I'd do would be to use:

open (LINKS, "nonactive/pages/$sela/$selb/$selc.d2k2page") || die "Failed to open nonactive/pages/$sela/$selb/$selc.d2k2page - $!\n";
my $line;
chomp ($line = <LINKS>);
close (LINKS);
my @pag;
if ($selc eq "index"){
@pag = split (/,/, $line);
}

Hope that helps a bit.