| 1 | package App::CSVUtils::csv_uniq; | ||||||||||||
| 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( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 13 | gen_csv_util | ||||||||||||
| 14 | 2 | ); | |||||||||||
| 15 | |||||||||||||
| 16 | gen_csv_util( | ||||||||||||
| 17 | name => 'csv_uniq', | ||||||||||||
| 18 | summary => 'Report or omit duplicated values in CSV', | ||||||||||||
| 19 | add_args => { | ||||||||||||
| 20 | %App::CSVUtils::argspec_fields_1plus, | ||||||||||||
| 21 | ignore_case => { | ||||||||||||
| 22 | summary => 'Ignore case when comparing', | ||||||||||||
| 23 | schema => 'true*', | ||||||||||||
| 24 | cmdline_aliases => {i=>{}}, | ||||||||||||
| 25 | }, | ||||||||||||
| 26 | unique => { | ||||||||||||
| 27 | summary => 'Instead of reporting duplicate values, report unique values instead', | ||||||||||||
| 28 | schema => 'true*', | ||||||||||||
| 29 | }, | ||||||||||||
| 30 | }, | ||||||||||||
| 31 | examples => [ | ||||||||||||
| 32 | { | ||||||||||||
| 33 | summary => 'Check that field "foo" in CSV is unique, compare case-insensitively, report duplicates', | ||||||||||||
| 34 | argv => ['file.csv', '-i', 'foo'], | ||||||||||||
| 35 | test => 0, | ||||||||||||
| 36 | 'x.doc.show_result' => 0, | ||||||||||||
| 37 | }, | ||||||||||||
| 38 | { | ||||||||||||
| 39 | summary => 'Check that combination of fields "foo", "bar", "baz" in CSV is unique, report duplicates', | ||||||||||||
| 40 | argv => ['file.csv', 'foo', 'bar', 'baz'], | ||||||||||||
| 41 | test => 0, | ||||||||||||
| 42 | 'x.doc.show_result' => 0, | ||||||||||||
| 43 | }, | ||||||||||||
| 44 | ], | ||||||||||||
| 45 | |||||||||||||
| 46 | writes_csv => 0, | ||||||||||||
| 47 | |||||||||||||
| 48 | tags => ['category:filtering'], | ||||||||||||
| 49 | |||||||||||||
| 50 | 6 | ▶ | on_input_header_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=2 SCAR=6.9
| |||||||||||||
| 51 | 6 | my $r = shift; | |||||||||||
| 52 | |||||||||||||
| 53 | # we add this key to the stash | ||||||||||||
| 54 | 6 | $r->{seen} = {}; | |||||||||||
| 55 | 6 | $r->{fields_idx} = []; | |||||||||||
| 56 | |||||||||||||
| 57 | # check arguments | ||||||||||||
| 58 | 6 | for my $field (@{ $r->{util_args}{fields} }) { | |||||||||||
| 59 | 8 | push @{ $r->{fields_idx} }, App::CSVUtils::_find_field($r->{input_fields}, $field); | |||||||||||
| 60 | } | ||||||||||||
| 61 | }, | ||||||||||||
| 62 | |||||||||||||
| 63 | 15 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=6 SCAR=17.9
| |||||||||||||
| 64 | 15 | my $r = shift; | |||||||||||
| 65 | |||||||||||||
| 66 | 15 | my @vals; | |||||||||||
| 67 | 15 | for my $field_idx (@{ $r->{fields_idx} }) { | |||||||||||
| 68 | 21 | ▶ | my $fieldval = $r->{input_row}[ $field_idx ] // ''; | ||||||||||
|
Condition50%1/2
$r->{"input_row"}[$field_idx] // ""
MC/DC0%0/1
$r->{"input_row"}[$field_idx] // ""
$r->{"input_row"}[$field_idx]
Truth table50%1/2
A
A= $r->{"input_row"}[$field_idx]
| |||||||||||||
| 69 | 21 | ▶ | push @vals, $r->{util_args}{ignore_case} ? lc($fieldval) : $fieldval; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | } | ||||||||||||
| 71 | 15 | my $val = join("|", @vals); | |||||||||||
| 72 | 15 | $r->{seen}{$val}++; | |||||||||||
| 73 | 15 | ▶ | unless ($r->{util_args}{unique}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | 12 | ▶ | print "csv-uniq: Duplicate value '$val'\n" if $r->{seen}{$val} == 2; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 75 | } | ||||||||||||
| 76 | }, | ||||||||||||
| 77 | |||||||||||||
| 78 | 5 | ▶ | on_end => sub { | ||||||||||
|
Subroutine
__ANON__: called CC=4 SCAR=13.9
| |||||||||||||
| 79 | 5 | my $r = shift; | |||||||||||
| 80 | |||||||||||||
| 81 | 5 | ▶ | if ($r->{util_args}{unique}) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 82 | 1 | for my $val (sort keys %{ $r->{seen} }) { | |||||||||||
| 83 | 3 | ▶ | print "csv-uniq: Unique value '$val'\n" if $r->{seen}{$val} == 1; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | } | ||||||||||||
| 85 | } | ||||||||||||
| 86 | }, | ||||||||||||
| 87 | 2 | ); | |||||||||||
| 88 | |||||||||||||
| 89 | 2 | 1; | |||||||||||
| 90 | # ABSTRACT: Report or omit duplicated values in CSV | ||||||||||||
| 91 | |||||||||||||
| 92 | __END__ | ||||||||||||