Data conversion
pll at lanminds.com
pll at lanminds.com
Wed Jan 8 13:29:58 EST 2003
In a message dated: 08 Jan 2003 13:01:05 EST
Ben Boulanger said:
>Read the entire thing into memory (does not scale for
>large files) or iterate over the file twice and just mark the maximum
>length. Anyone else have a better idea on how to find the max length of
>a field in a CSV file?
Why do you need to iterate over the file twice? Can't you just do
something like:
$file='foo.csv';
open (FILE, "$file") or die "Can't open $file: $!\n";
$longest_field=0;
$longest_length=0
while (<FILE>) {
@fields=split (/,/,$_);
for ($i=0; $i <$#fields; $i++) {
$length = length($fields[$i]);
if ($length > $longest_length) {
$longest_field = $i
$longest_length = $length;
}
}
}
That way you only iterate over the file once.
--
Seeya,
Paul
--
Key fingerprint = 1660 FECC 5D21 D286 F853 E808 BB07 9239 53F1 28EE
It may look like I'm just sitting here doing nothing,
but I'm really actively waiting for all my problems to go away.
If you're not having fun, you're not doing it right!
More information about the gnhlug-discuss
mailing list