| 1 | package App::CSVUtils::csv_find_values; | ||||||||||||||||||||||||||||
| 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( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 13 | gen_csv_util | ||||||||||||||||||||||||||||
| 14 | 1 | ); | |||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||
| 16 | gen_csv_util( | ||||||||||||||||||||||||||||
| 17 | name => 'csv_find_values', | ||||||||||||||||||||||||||||
| 18 | summary => 'Find specified values in a CSV field', | ||||||||||||||||||||||||||||
| 19 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | Example input: | ||||||||||||||||||||||||||||
| 22 | |||||||||||||||||||||||||||||
| 23 | # product.csv | ||||||||||||||||||||||||||||
| 24 | sku,name,is_active,description | ||||||||||||||||||||||||||||
| 25 | SKU1,foo,1,blah | ||||||||||||||||||||||||||||
| 26 | SK2,bar,1,blah | ||||||||||||||||||||||||||||
| 27 | SK3B,baz,0,blah | ||||||||||||||||||||||||||||
| 28 | SKU2,qux,1,blah | ||||||||||||||||||||||||||||
| 29 | SKU3,quux,1,blah | ||||||||||||||||||||||||||||
| 30 | SKU14,corge,0,blah | ||||||||||||||||||||||||||||
| 31 | |||||||||||||||||||||||||||||
| 32 | Check whether specified values are found in the `sku` field, print message when | ||||||||||||||||||||||||||||
| 33 | they are (search case-insensitively): | ||||||||||||||||||||||||||||
| 34 | |||||||||||||||||||||||||||||
| 35 | % csv-find-values product.csv sku sku1 sk3b sku15 -i | ||||||||||||||||||||||||||||
| 36 | 'sku1' is found in field 'sku' row 2 | ||||||||||||||||||||||||||||
| 37 | 'sk3b' is found in field 'sku' row 4 | ||||||||||||||||||||||||||||
| 38 | |||||||||||||||||||||||||||||
| 39 | Print message when values are *not* found instead: | ||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||
| 41 | % csv-find-values product.csv sku sku1 sk3b sku15 -i --print-when=not_found | ||||||||||||||||||||||||||||
| 42 | 'sku15' is NOT found in field 'sku' | ||||||||||||||||||||||||||||
| 43 | |||||||||||||||||||||||||||||
| 44 | Always print message: | ||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | % csv-find-values product.csv sku sku1 sk3b sku15 -i --print-when=always | ||||||||||||||||||||||||||||
| 47 | 'sku1' is found in field 'sku' row 2 | ||||||||||||||||||||||||||||
| 48 | 'sk3b' is found in field 'sku' row 4 | ||||||||||||||||||||||||||||
| 49 | 'sku15' is NOT found in field 'sku' | ||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | Do custom action with Perl code, code will receive `$_` (the value being | ||||||||||||||||||||||||||||
| 52 | evaluated), `$found` (bool, whether it is found in the field), `$rownum` (the | ||||||||||||||||||||||||||||
| 53 | row number the value is found in), `$data_rownum` (the data row number the value | ||||||||||||||||||||||||||||
| 54 | is found in, equals `$rownum` - 1): | ||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | % csv-find-values product.csv sku1 sk3b sku15 -i -e 'if ($found) { print "$_ found\n" } else { print "$_ NOT found\n" }' | ||||||||||||||||||||||||||||
| 57 | sku1 found | ||||||||||||||||||||||||||||
| 58 | sk3b found | ||||||||||||||||||||||||||||
| 59 | sku15 NOT found | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | There is an option to do fuzzy matching, where similar values will be suggested | ||||||||||||||||||||||||||||
| 62 | when exact match is not found. | ||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||
| 64 | MARKDOWN | ||||||||||||||||||||||||||||
| 65 | add_args => { | ||||||||||||||||||||||||||||
| 66 | ignore_case => { | ||||||||||||||||||||||||||||
| 67 | schema => 'bool*', | ||||||||||||||||||||||||||||
| 68 | cmdline_aliases => {ci=>{}, i=>{}}, | ||||||||||||||||||||||||||||
| 69 | tags => ['category:searching'], | ||||||||||||||||||||||||||||
| 70 | }, | ||||||||||||||||||||||||||||
| 71 | fuzzy => { | ||||||||||||||||||||||||||||
| 72 | schema => 'true*', | ||||||||||||||||||||||||||||
| 73 | tags => ['category:searching'], | ||||||||||||||||||||||||||||
| 74 | }, | ||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||
| 76 | %App::CSVUtils::argspec_field_1, | ||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||
| 78 | values => { | ||||||||||||||||||||||||||||
| 79 | 'x.name.is_plural' => 1, | ||||||||||||||||||||||||||||
| 80 | 'x.name.singular' => 'value', | ||||||||||||||||||||||||||||
| 81 | schema => ['array*', of=>'str*', min_len=>1], | ||||||||||||||||||||||||||||
| 82 | req => 1, | ||||||||||||||||||||||||||||
| 83 | pos => 2, | ||||||||||||||||||||||||||||
| 84 | slurpy => 1, | ||||||||||||||||||||||||||||
| 85 | }, | ||||||||||||||||||||||||||||
| 86 | print_when => { | ||||||||||||||||||||||||||||
| 87 | schema => ['str*', in=>[qw/found not_found always/]], | ||||||||||||||||||||||||||||
| 88 | default => 'found', | ||||||||||||||||||||||||||||
| 89 | description => <<'MARKDOWN', | ||||||||||||||||||||||||||||
| 90 | |||||||||||||||||||||||||||||
| 91 | Overriden by the `--eval` option. | ||||||||||||||||||||||||||||
| 92 | |||||||||||||||||||||||||||||
| 93 | MARKDOWN | ||||||||||||||||||||||||||||
| 94 | tags => ['category:output'], | ||||||||||||||||||||||||||||
| 95 | }, | ||||||||||||||||||||||||||||
| 96 | %App::CSVUtils::argspecopt_eval, | ||||||||||||||||||||||||||||
| 97 | }, | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | writes_csv => 0, | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | tags => ['category:searching'], | ||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 0 | ▶ | on_input_header_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 104 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 105 | |||||||||||||||||||||||||||||
| 106 | # check arguments | ||||||||||||||||||||||||||||
| 107 | 0 | my $field = $r->{util_args}{field}; | |||||||||||||||||||||||||||
| 108 | 0 | my $field_idx = App::CSVUtils::_select_field($r->{input_fields}, $field); | |||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||
| 110 | # we add the following keys to the stash | ||||||||||||||||||||||||||||
| 111 | 0 | $r->{field} = $field; | |||||||||||||||||||||||||||
| 112 | 0 | $r->{field_idx} = $field_idx; | |||||||||||||||||||||||||||
| 113 | 0 | ▶ | $r->{code} = compile_eval_code($r->{util_args}{eval}, 'eval') if defined $r->{util_args}{eval}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 114 | 0 | $r->{csv_values} = []; | |||||||||||||||||||||||||||
| 115 | $r->{search_values} = $r->{util_args}{ignore_case} ? | ||||||||||||||||||||||||||||
| 116 | 0 | ▶ | [ map { lc } @{ $r->{util_args}{values} }] : $r->{util_args}{values}; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 117 | }, | ||||||||||||||||||||||||||||
| 118 | |||||||||||||||||||||||||||||
| 119 | 0 | ▶ | on_input_data_row => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||||||||||||||||||
| 120 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||
| 122 | 0 | ▶ | my $val = ($r->{input_row}[ $r->{field_idx} ] // ''); | ||||||||||||||||||||||||||
|
Condition0%0/2
$r->{"input_row"}[$r->{"field_idx"}] // ""
MC/DC0%0/1
$r->{"input_row"}[$r->{"field_idx"}] // ""
$r->{"input_row"}[$r->{"field_idx"}]
Truth table0%0/2
A
A= $r->{"input_row"}[$r->{"field_idx"}]
| |||||||||||||||||||||||||||||
| 123 | 0 | ▶ | if ($r->{util_args}{ignore_case}) { $val = lc $val } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 124 | 0 | push @{ $r->{csv_values} }, $val; | |||||||||||||||||||||||||||
| 125 | }, | ||||||||||||||||||||||||||||
| 126 | |||||||||||||||||||||||||||||
| 127 | 0 | ▶ | after_close_input_files => sub { | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=15 SCAR=54.8
| |||||||||||||||||||||||||||||
| 128 | 0 | my $r = shift; | |||||||||||||||||||||||||||
| 129 | |||||||||||||||||||||||||||||
| 130 | 0 | my $ci = $r->{util_args}{ignore_case}; | |||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 0 | my $maxdist; | |||||||||||||||||||||||||||
| 133 | 0 | for my $i (0 .. $#{ $r->{util_args}{values} }) { | |||||||||||||||||||||||||||
| 134 | 0 | my $value = $r->{util_args}{values}[$i]; | |||||||||||||||||||||||||||
| 135 | 0 | my $search_value = $r->{search_values}[$i]; | |||||||||||||||||||||||||||
| 136 | 0 | my $found_rownum; | |||||||||||||||||||||||||||
| 137 | |||||||||||||||||||||||||||||
| 138 | 0 | my $j = 0; | |||||||||||||||||||||||||||
| 139 | 0 | for my $v (@{ $r->{csv_values} }) { | |||||||||||||||||||||||||||
| 140 | 0 | $j++; | |||||||||||||||||||||||||||
| 141 | 0 | ▶ | if ($v eq $search_value) { $found_rownum = $j; last } | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 142 | } | ||||||||||||||||||||||||||||
| 143 | |||||||||||||||||||||||||||||
| 144 | 0 | my $suggested_values; | |||||||||||||||||||||||||||
| 145 | 0 | ▶ | if (!defined($found_rownum) && $r->{util_args}{fuzzy}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not defined $found_rownum and $r->{"util_args"}{"fuzzy"}
MC/DC0%0/2
not defined $found_rownum and $r->{"util_args"}{"fuzzy"}
not defined $found_rownum
$r->{"util_args"}{"fuzzy"}
Truth table0%0/3
not(A) and B
A= not defined $found_rownumB= $r->{"util_args"}{"fuzzy"}
| |||||||||||||||||||||||||||||
| 146 | # XXX with this, we do exact matching twice | ||||||||||||||||||||||||||||
| 147 | 0 | require Complete::Util; | |||||||||||||||||||||||||||
| 148 | 0 | local $Complete::Common::OPT_CI = 1; | |||||||||||||||||||||||||||
| 149 | 0 | local $Complete::Common::OPT_MAP_CASE = 0; | |||||||||||||||||||||||||||
| 150 | 0 | local $Complete::Common::OPT_WORD_MODE = 0; | |||||||||||||||||||||||||||
| 151 | 0 | local $Complete::Common::OPT_CHAR_MODE = 0; | |||||||||||||||||||||||||||
| 152 | 0 | local $Complete::Common::OPT_FUZZY = 1; | |||||||||||||||||||||||||||
| 153 | $suggested_values = Complete::Util::complete_array_elem( | ||||||||||||||||||||||||||||
| 154 | array => $r->{csv_values}, | ||||||||||||||||||||||||||||
| 155 | 0 | word => $value, | |||||||||||||||||||||||||||
| 156 | ); | ||||||||||||||||||||||||||||
| 157 | } | ||||||||||||||||||||||||||||
| 158 | |||||||||||||||||||||||||||||
| 159 | 0 | ▶ | if ($r->{code}) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 160 | { | ||||||||||||||||||||||||||||
| 161 | 0 | local $_ = $value; | |||||||||||||||||||||||||||
| 162 | 0 | ▶ | local $main::found = defined $found_rownum ? 1:0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 163 | 0 | local $main::rownum = $found_rownum+1; | |||||||||||||||||||||||||||
| 164 | 0 | local $main::data_rownum = $found_rownum; | |||||||||||||||||||||||||||
| 165 | 0 | local $main::r = $r; | |||||||||||||||||||||||||||
| 166 | 0 | local $main::csv = $r->{input_parser}; | |||||||||||||||||||||||||||
| 167 | 0 | $r->{code}->($_); | |||||||||||||||||||||||||||
| 168 | } | ||||||||||||||||||||||||||||
| 169 | } else { | ||||||||||||||||||||||||||||
| 170 | 0 | ▶ | if (defined $found_rownum) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 171 | 0 | ▶ | if ($r->{util_args}{print_when} eq 'found' || $r->{util_args}{print_when} eq 'always') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"util_args"}{"print_when"} eq "found" or $r->{"util_args"}{"print_when"} eq "always"
MC/DC0%0/2
$r->{"util_args"}{"print_when"} eq "found" or $r->{"util_args"}{"print_when"} eq "always"
$r->{"util_args"}{"print_when"} eq "found"
$r->{"util_args"}{"print_when"} eq "always"
Truth table0%0/3
A or B
A= $r->{"util_args"}{"print_when"} eq "found"B= $r->{"util_args"}{"print_when"} eq "always"
| |||||||||||||||||||||||||||||
| 172 | 0 | print "'$value' is found in field '$r->{field}' row ".($found_rownum+1)."\n"; | |||||||||||||||||||||||||||
| 173 | } | ||||||||||||||||||||||||||||
| 174 | } else { | ||||||||||||||||||||||||||||
| 175 | 0 | ▶ | if ($r->{util_args}{print_when} eq 'not_found' || $r->{util_args}{print_when} eq 'always') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$r->{"util_args"}{"print_when"} eq "not_found" or $r->{"util_args"}{"print_when"} eq "always"
MC/DC0%0/2
$r->{"util_args"}{"print_when"} eq "not_found" or $r->{"util_args"}{"print_when"} eq "always"
$r->{"util_args"}{"print_when"} eq "not_found"
$r->{"util_args"}{"print_when"} eq "always"
Truth table0%0/3
A or B
A= $r->{"util_args"}{"print_when"} eq "not_found"B= $r->{"util_args"}{"print_when"} eq "always"
| |||||||||||||||||||||||||||||
| 176 | 0 | ▶ | print "'$value' is NOT found in field '$r->{field}'".($suggested_values && @$suggested_values ? ", perhaps you meant ".join("/", @$suggested_values)."?" : "")."\n"; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
$suggested_values and @$suggested_values
MC/DC0%0/2
$suggested_values and @$suggested_values
$suggested_values
@$suggested_values
Truth table0%0/3
A and B
A= $suggested_valuesB= @$suggested_values
| |||||||||||||||||||||||||||||
| 177 | } | ||||||||||||||||||||||||||||
| 178 | } | ||||||||||||||||||||||||||||
| 179 | } | ||||||||||||||||||||||||||||
| 180 | } | ||||||||||||||||||||||||||||
| 181 | }, | ||||||||||||||||||||||||||||
| 182 | 1 | ); | |||||||||||||||||||||||||||
| 183 | |||||||||||||||||||||||||||||
| 184 | 1 | 1; | |||||||||||||||||||||||||||
| 185 | # ABSTRACT: Find specified values in a CSV field | ||||||||||||||||||||||||||||
| 186 | |||||||||||||||||||||||||||||
| 187 | __END__ | ||||||||||||||||||||||||||||