on Mon, 02 Sep 2002 13:15:56 GMT, [EMAIL PROTECTED]
(Sl003b0462) wrote:
> I have a csv file with column headings. As the file may be amended
> and the columns reordered I wish to read the headings and
> determine their location on each pass. How do I generate a list of
> fixed headings and determine their position, before extracting the
> corresponding field for each line.
You don't say whether you are using a module for your csv parsing, so
I assume you are rolling your own.
You could use an array of hashes for your data:
#! perl -w
use strict;
# warning: very simplistic csv parsing:
# no embedded separator or quoting allowed
# and no data checking
# get header fields
my @headers = split /[,\n]/, <DATA>;
#get data
my @data = ();
while (<DATA>) {
my %dataline = ();
@dataline{@headers} = split /[,\n]/, $_;
push @data, \%dataline;
}
print Dumper(\@data);
__DATA__
field1,field2,field3,field4
1,abc,def,2
3,ghi,jkl,4
5,mno,pqr,6
--
felix
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]