How to handle fields names in comma separated file?
How to handle fields names in comma separated file?
am 06.11.2007 04:28:24 von some
I have a comma separated file with hundreds of field names. For the sake
of brevity, let's assume there are only a name, address, city, and state
field. I currently have the field names defined as scalars in an array
like:
my @fields = (
my $NAME,
my $ADDRESS,
my $CITY,
my $STATE
);
This allows me to assign all of the fields to an array element in one
line as with the split function below. The problem with this is that
the field names are then known only as $fields[0], $fields[1], etc.
Is there a better way to handle field names in a csv file?
while ( ) {
chomp;
my(@fields) = split /\,/;
print "$fields[0]\n";
print "$fields[1]\n";
print "$fields[2]\n";
print "$fields[3]\n";
Re: How to handle fields names in comma separated file?
am 06.11.2007 06:04:42 von ramesh.thangamani
somebody wrote:
> I have a comma separated file with hundreds of field names. For the sake
> of brevity, let's assume there are only a name, address, city, and state
> field. I currently have the field names defined as scalars in an array
> like:
>
> my @fields = (
> my $NAME,
> my $ADDRESS,
> my $CITY,
> my $STATE
> );
>
>
> This allows me to assign all of the fields to an array element in one
> line as with the split function below. The problem with this is that
> the field names are then known only as $fields[0], $fields[1], etc.
> Is there a better way to handle field names in a csv file?
>
>
> while ( ) {
>
> chomp;
>
> my(@fields) = split /\,/;
>
> print "$fields[0]\n";
> print "$fields[1]\n";
> print "$fields[2]\n";
> print "$fields[3]\n";
You can store them in a hash that is better:
like
my %hash;
$hash{NAME} = $fields[0];
etc
Re: How to handle fields names in comma separated file?
am 06.11.2007 07:50:58 von Ben Morrow
Quoth somebody :
>
> I have a comma separated file with hundreds of field names. For the sake
> of brevity, let's assume there are only a name, address, city, and state
> field. I currently have the field names defined as scalars in an array
> like:
>
> my @fields = (
> my $NAME,
> my $ADDRESS,
> my $CITY,
> my $STATE
> );
I'm not sure what you think this does, but it almost certainly
doesn't... There is no association between the variables and the
elements of the array after this statement, they just all happen to
contain undef.
> This allows me to assign all of the fields to an array element in one
> line as with the split function below. The problem with this is that
> the field names are then known only as $fields[0], $fields[1], etc.
> Is there a better way to handle field names in a csv file?
>
> while ( ) {
>
> chomp;
>
> my(@fields) = split /\,/;
Comma is not special in a regex, so there's no need to escape it.
You should parse CSV with a module, such as Text::CSV_XS, that knows how
to handle quoting. CSV is remarkably hard to parse correctly for such an
apparently simple format...
If you want access by name, you can use a hash:
my @fields = qw/NAME ADDRESS CITY STATE/;
my %fields = split /,/; # or something better
print $fields{NAME};
> print "$fields[0]\n";
If you set $\ you don't need all those "\n"s. If you're printing
multi-line data it's better to use a heredoc.
Ben
Re: How to handle fields names in comma separated file?
am 06.11.2007 13:02:19 von Tad McClellan
rthangam wrote:
>
> somebody wrote:
>> I have a comma separated file with hundreds of field names. For the sake
>> of brevity, let's assume there are only a name, address, city, and state
>> field. I currently have the field names defined as scalars in an array
>> like:
>>
>> my @fields = (
>> my $NAME,
>> my $ADDRESS,
>> my $CITY,
>> my $STATE
>> );
>>
>>
>> This allows me to assign all of the fields to an array element in one
>> line as with the split function below. The problem with this is that
>> the field names are then known only as $fields[0], $fields[1], etc.
>> Is there a better way to handle field names in a csv file?
>>
>>
>> while ( ) {
>>
>> chomp;
>>
>> my(@fields) = split /\,/;
>>
>> print "$fields[0]\n";
>> print "$fields[1]\n";
>> print "$fields[2]\n";
>> print "$fields[3]\n";
>
> You can store them in a hash that is better:
>
> like
>
> my %hash;
>
> $hash{NAME} = $fields[0];
And use a "hash slice" (perldoc perldata) to load them all up in one go:
my @fnames = qw/name address city state/;
my %record;
@record{ @fnames } = split /,/;
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
Re: How to handle fields names in comma separated file?
am 06.11.2007 16:46:13 von Ben Morrow
Quoth Ben Morrow :
>
> If you want access by name, you can use a hash:
>
> my @fields = qw/NAME ADDRESS CITY STATE/;
> my %fields = split /,/; # or something better
Sorry, stupid thinko; that should read
my @fields = qw/NAME ADDRESS CITY STATE/;
my %fields;
@fields{@fields} = split /,/; # or something better
Note that @fields{} actually refers to %fields, not to @fields. There is
some sense there somewhere, but it's quite hard to find... :)
Ben
Re: How to handle fields names in comma separated file?
am 07.11.2007 03:09:10 von Tad McClellan
Ben Morrow wrote:
>
> Quoth Ben Morrow :
>>
>> If you want access by name, you can use a hash:
>>
>> my @fields = qw/NAME ADDRESS CITY STATE/;
>> my %fields = split /,/; # or something better
>
> Sorry, stupid thinko; that should read
>
> my @fields = qw/NAME ADDRESS CITY STATE/;
> my %fields;
> @fields{@fields} = split /,/; # or something better
>
> Note that @fields{} actually refers to %fields, not to @fields. There is
> some sense there somewhere, but it's quite hard to find... :)
Not if you think about it the right way though.
Dollar-sign is singular and at-sign is plural.
A hash slice is a list, which is plural, so there should be
an at-sign somewhere.
:-)
--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"