Greg London wrote:
AHR! typo!
> open(my $fh, $filename);
>
> # get first line with field names.
> my $line;
> unless(defined($line=<$fh>))
> {
> die "file is empty";
> }
>
> my @fields_by_order = split(/\t/, $line);
>
> # read rest of file, line by line,
> # and pull out the data for a single record.
>
> my @master_records;
>
> while(<$fh>)
> {
> my @data_by_order = split(/\t/, $_);
>
> my %data_by_field;
>
> # put the data into the hash
> # data will appear in same order as
> # the order of fields on first line.
> for(my $i=0;$i<scalar(@fields_by_order);$i++)
> {
> $data_by_field{$fields_by_order[$i]} =
> $data_by_field[$i];
$data_by_order[$i]; # CORRECTION!!!
> }
>
> # hash data_by_field can now access the
> # data for this line by field name.
> # i.e. to get the name of the current record:
> my $name = $data_by_field{Name};
>
> print "$name\n";
>
> # push hash ref into global storage
> # if you want (and if you have the memory)
> push(@master_records,\%data_by_field);
> }
>
> close($fh);
>
> # later, if you need to process all the records at once,
> # then @master_records is an array of hashes,
> # each hash is one record.
>
> foreach my $record (@master_records)
> {
> my $name = $record->{Name};
> print "$name\n";
> }
>
> hope this helps,
> Greg
--
Greg London
x7541