| 1 | package App::CSVUtils::csv_convert_to_hash; | ||||||||||||
| 2 | |||||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 4 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 5 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | |||||||||||||
| 7 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 8 | 2 | our $DATE = '2026-07-09'; # DATE | |||||||||||
| 9 | 2 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||
| 10 | 2 | our $VERSION = '1.038'; # VERSION | |||||||||||
| 11 | |||||||||||||
| 12 | 2 | ▶ | use App::CSVUtils qw(gen_csv_util); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | |||||||||||||
| 14 | gen_csv_util( | ||||||||||||
| 15 | name => 'csv_convert_to_hash', | ||||||||||||
| 16 | summary => 'Return a hash of field names as keys and first data row as values', | ||||||||||||
| 17 | description => <<'MARKDOWN', | ||||||||||||
| 18 | |||||||||||||
| 19 | MARKDOWN | ||||||||||||
| 20 | |||||||||||||
| 21 | add_args => { | ||||||||||||
| 22 | rownum => { | ||||||||||||
| 23 | schema => 'posint*', | ||||||||||||
| 24 | default => 1, | ||||||||||||
| 25 | summary => 'Row number (e.g. 1 for first data row)', | ||||||||||||
| 26 | pos => 1, | ||||||||||||
| 27 | }, | ||||||||||||
| 28 | }, | ||||||||||||
| 29 | tags => ['category:converting', 'outputs-data-structure'], | ||||||||||||
| 30 | |||||||||||||
| 31 | examples => [ | ||||||||||||
| 32 | { | ||||||||||||
| 33 | summary => 'Create a table containing field name as keys and second row as values', | ||||||||||||
| 34 | argv => ['file.csv', 2], | ||||||||||||
| 35 | test => 0, | ||||||||||||
| 36 | 'x.doc.show_result' => 0, | ||||||||||||
| 37 | }, | ||||||||||||
| 38 | ], | ||||||||||||
| 39 | |||||||||||||
| 40 | 3 | ▶ | on_begin => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 41 | 3 | my $r = shift; | |||||||||||
| 42 | |||||||||||||
| 43 | # check arguments & set defaults | ||||||||||||
| 44 | 3 | ▶ | $r->{util_args}{rownum} //= 1; | ||||||||||
|
Condition100%2/2
$r->{"util_args"}{"rownum"} //= 1
MC/DC100%1/1
$r->{"util_args"}{"rownum"} //= 1
$r->{"util_args"}{"rownum"}
Truth table100%2/2
A
A= $r->{"util_args"}{"rownum"}
| |||||||||||||
| 45 | }, | ||||||||||||
| 46 | |||||||||||||
| 47 | 3 | ▶ | on_input_header_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||
| 48 | 3 | my $r = shift; | |||||||||||
| 49 | |||||||||||||
| 50 | 3 | $r->{result} = [200, "OK", { map { $_ => undef } @{ $r->{input_fields} } }]; | |||||||||||
| 51 | }, | ||||||||||||
| 52 | |||||||||||||
| 53 | 6 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 54 | 6 | my $r = shift; | |||||||||||
| 55 | |||||||||||||
| 56 | 6 | ▶ | if ($r->{input_data_rownum} == $r->{util_args}{rownum}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 57 | 2 | $r->{result} = [200, "OK", { map { $_ => $r->{input_row}[ $r->{input_fields_idx}{$_} ] } @{ $r->{input_fields} } }]; | |||||||||||
| 58 | 2 | $r->{wants_skip_file}++; | |||||||||||
| 59 | } | ||||||||||||
| 60 | }, | ||||||||||||
| 61 | |||||||||||||
| 62 | 2 | writes_csv => 0, | |||||||||||
| 63 | ); | ||||||||||||
| 64 | |||||||||||||
| 65 | 2 | 1; | |||||||||||
| 66 | # ABSTRACT: Return a hash of field names as keys and first data row as values | ||||||||||||
| 67 | |||||||||||||
| 68 | __END__ | ||||||||||||