Sherlock Perl has asked for the wisdom of the Perl Monks concerning the following question:
If I wasn't clear I just one to create (or add to this one) a function that returns the value in A1 from a .csv file.sub hlookup { my $csv_file = shift; my $field_name = shift; my $row_num = shift; my $csv = Text::CSV_XS->new ( { binary => 1 } ) # should set bin +ary attribute. or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, "<:encoding(utf8)", $csv_file or die "test.csv: $!"; my $field_num; my $first_row = $csv->getline_all( $fh ,3, 1 ); $csv->column_names(@$first_row); my $all = $csv->getline_hr_all($fh, $row_num - 2, 1); # - 2 since array indexes are 0-based, # and we count our lines 1-based, # and because we've already read the first # line. return $all->[0]->{$field_name} }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Parse .csv file
by kcott (Archbishop) on Jul 10, 2012 at 19:45 UTC | |
by Sherlock Perl (Novice) on Jul 10, 2012 at 21:01 UTC | |
by kcott (Archbishop) on Jul 10, 2012 at 22:48 UTC | |
by aitap (Curate) on Jul 10, 2012 at 21:44 UTC | |
|
Re: Parse .csv file
by frozenwithjoy (Priest) on Jul 10, 2012 at 19:48 UTC |