| 1 | package App::CSVUtils::csv_get_cells; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | |||||||||||||||||||||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||||||||||||||||||
| 8 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||||||||||||||||||
| 9 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||||||||||||||||||
| 10 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | ▶ | use App::CSVUtils qw(gen_csv_util); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||
| 14 | gen_csv_util( | ||||||||||||||||||||||||||||
| 15 | name => 'csv_get_cells', | ||||||||||||||||||||||||||||
| 16 | summary => 'Get one or more cells from CSV', | ||||||||||||||||||||||||||||
| 17 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | This utility lets you specify "coordinates" of cell locations to extract. Each | ||||||||||||||||||||||||||||
| 20 | coordinate is in the form of `<field>,<row>` where `<field>` is the field name | ||||||||||||||||||||||||||||
| 21 | or position (1-based, so 1 is the first field) and `<row>` is the row position | ||||||||||||||||||||||||||||
| 22 | (1-based, so 1 is the header row and 2 is the first data row). | ||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | MARKDOWN | ||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||
| 26 | add_args => { | ||||||||||||||||||||||||||||
| 27 | coordinates => { | ||||||||||||||||||||||||||||
| 28 | 'x.name.is_plural' => 1, | ||||||||||||||||||||||||||||
| 29 | 'x.name.singular' => 'coordinate', | ||||||||||||||||||||||||||||
| 30 | summary => 'List of coordinates, each in the form of <col>,<row> e.g. age,1 or 1,1', | ||||||||||||||||||||||||||||
| 31 | schema => ['array*', of=>'str*', min_len=>1], | ||||||||||||||||||||||||||||
| 32 | req => 1, | ||||||||||||||||||||||||||||
| 33 | pos => 1, | ||||||||||||||||||||||||||||
| 34 | slurpy => 1, | ||||||||||||||||||||||||||||
| 35 | }, | ||||||||||||||||||||||||||||
| 36 | }, | ||||||||||||||||||||||||||||
| 37 | examples => [ | ||||||||||||||||||||||||||||
| 38 | { | ||||||||||||||||||||||||||||
| 39 | summary => 'Get the age for second row', | ||||||||||||||||||||||||||||
| 40 | argv => ['file.csv', 'age,2'], | ||||||||||||||||||||||||||||
| 41 | test => 0, | ||||||||||||||||||||||||||||
| 42 | 'x.doc.show_result' => 0, | ||||||||||||||||||||||||||||
| 43 | }, | ||||||||||||||||||||||||||||
| 44 | ], | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | writes_csv => 0, | ||||||||||||||||||||||||||||
| 47 | |||||||||||||||||||||||||||||
| 48 | tags => ['category:extracting'], | ||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||
| 50 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=10 SCAR=47.0
| |||||||||||||||||||||||||||||
| 51 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | # this is the key we add to stash | ||||||||||||||||||||||||||||
| 54 | 0 | ▶ | $r->{cells} //= []; | ||||||||||||||||||||||||||
|
Condition0%0/2
$r->{"cells"} //= []
MC/DC0%0/1
$r->{"cells"} //= []
$r->{"cells"}
Truth table0%0/2
A
A= $r->{"cells"}
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | 0 | my $j = -1; | |||||||||||||||||||||||||||
| 57 | COORD: | ||||||||||||||||||||||||||||
| 58 | 0 | for my $coord (@{ $r->{util_args}{coordinates} }) { | |||||||||||||||||||||||||||
| 59 | 0 | $j++; | |||||||||||||||||||||||||||
| 60 | 0 | ▶ | my ($coord_field, $coord_row) = $coord =~ /\A(.+),(.+)\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 61 | or die [400, "Invalid coordinate '$coord': must be in field,row form"]; | ||||||||||||||||||||||||||||
| 62 | 0 | ▶ | $coord_row =~ /\A[0-9]+\z/ | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 63 | or die [400, "Invalid coordinate '$coord': invalid row syntax '$coord_row', must be a number"]; | ||||||||||||||||||||||||||||
| 64 | 0 | my $row; | |||||||||||||||||||||||||||
| 65 | 0 | ▶ | if ($coord_row == 1) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 66 | 0 | $row = $r->{input_fields}; | |||||||||||||||||||||||||||
| 67 | } elsif ($coord_row == $r->{input_rownum}) { | ||||||||||||||||||||||||||||
| 68 | 0 | $row = $r->{input_row}; | |||||||||||||||||||||||||||
| 69 | } else { | ||||||||||||||||||||||||||||
| 70 | 0 | next COORD; | |||||||||||||||||||||||||||
| 71 | } | ||||||||||||||||||||||||||||
| 72 | |||||||||||||||||||||||||||||
| 73 | 0 | ▶ | if ($coord_field =~ /\A[0-9]+\z/) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | 0 | $coord_field >= 1 && $coord_field <= $#{ $r->{input_fields} }+1 | |||||||||||||||||||||||||||
| 75 | 0 | ▶ | or die [400, "Invalid coordinate '$coord': field number '$coord_field' out of bound, must be between 1-". ($#{$r->{input_fields}}+1)]; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$coord_field >= 1 and $coord_field <= $#{$$r{"input_fields"};} + 1
MC/DC0%0/2
$coord_field >= 1 and $coord_field <= $#{$$r{"input_fields"};} + 1
$coord_field >= 1
$coord_field <= $#{$$r{"input_fields"};} + 1
Truth table0%0/3
A and B
A= $coord_field >= 1B= $coord_field <= $#{$$r{"input_fields"};} + 1
| |||||||||||||||||||||||||||||
| 76 | 0 | $r->{cells}[$j] = $row->[$coord_field-1]; | |||||||||||||||||||||||||||
| 77 | } else { | ||||||||||||||||||||||||||||
| 78 | 0 | my $field_idx = App::CSVUtils::_find_field($r->{input_fields}, $coord_field); | |||||||||||||||||||||||||||
| 79 | 0 | $r->{cells}[$j] = $row->[ $field_idx ]; | |||||||||||||||||||||||||||
| 80 | } | ||||||||||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||||||||
| 82 | }, | ||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||
| 84 | 0 | ▶ | after_close_input_files => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||||||||||||||||
| 85 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 86 | |||||||||||||||||||||||||||||
| 87 | 0 | $r->{result} = [200, "OK", $r->{cells}]; | |||||||||||||||||||||||||||
| 88 | }, | ||||||||||||||||||||||||||||
| 89 | 1 | ); | |||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | 1 | 1; | |||||||||||||||||||||||||||
| 92 | # ABSTRACT: Get one or more cells from CSV | ||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||