Read a single line from a one-line text file in CSV format

111 views Asked by At

I am attempting to read a line from a single-line text file containing three numbers separated by commas. It would be like this:

40045,47250,400

I have been fussing with this for several days by trying different combinations I have found on the web. Below is what I have:

open(my $fn, '<', 'prgcfg.txt' or die "Could not open configuration file: '$fn' $!");
my $line = <$fn>;
chomp $line; 
close $fn; 
my ($n,$e,$g) = split /,/,$line[1];

The three scalars in the last line are declared above this. It always generates an error regardless of what I try. Any ideas?

Thank you!

1

There are 1 answers

0
toolic On BEST ANSWER

Assuming your error and warning messages originated from use strict; use warnings;, you can eliminate them with the following changes:

use warnings;
use strict;

open my $fn, '<', 'prgcfg.txt' or die "Could not open configuration file $!";
my $line = <$fn>;
chomp $line; 
close $fn; 
my ($n,$e,$g) = split /,/, $line;

The parentheses for open are misplaced; in fact, you don't need them at all.

$fn is a file handle, not a file name. Do not try to print the handle in the die message.

$line is a scalar; don't try to access an element of it as if it were an array.