| 1 | package App::CSVUtils::csv_shuf_rows; | ||||||||||||
| 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 | 1 | ▶ | use Log::ger; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 7 | |||||||||||||
| 8 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||||
| 9 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||||
| 10 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||||
| 11 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||||
| 12 | |||||||||||||
| 13 | 1 | ▶ | use App::CSVUtils qw( | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 14 | gen_csv_util | ||||||||||||
| 15 | 1 | ); | |||||||||||
| 16 | |||||||||||||
| 17 | gen_csv_util( | ||||||||||||
| 18 | name => 'csv_shuf_rows', | ||||||||||||
| 19 | summary => 'Shuffle CSV rows', | ||||||||||||
| 20 | description => <<'MARKDOWN', | ||||||||||||
| 21 | |||||||||||||
| 22 | This utility shuffle the rows in the CSV. Example input CSV: | ||||||||||||
| 23 | |||||||||||||
| 24 | name,age | ||||||||||||
| 25 | Andy,20 | ||||||||||||
| 26 | Dennis,15 | ||||||||||||
| 27 | Ben,30 | ||||||||||||
| 28 | Jerry,30 | ||||||||||||
| 29 | |||||||||||||
| 30 | Example output CSV: | ||||||||||||
| 31 | |||||||||||||
| 32 | name,age | ||||||||||||
| 33 | Ben,30 | ||||||||||||
| 34 | Andy,20 | ||||||||||||
| 35 | Jerry,30 | ||||||||||||
| 36 | Dennis,15 | ||||||||||||
| 37 | |||||||||||||
| 38 | MARKDOWN | ||||||||||||
| 39 | |||||||||||||
| 40 | add_args => { | ||||||||||||
| 41 | }, | ||||||||||||
| 42 | |||||||||||||
| 43 | tags => ['category:sorting'], | ||||||||||||
| 44 | |||||||||||||
| 45 | 0 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 46 | 0 | my $r = shift; | |||||||||||
| 47 | |||||||||||||
| 48 | # keys we add to the stash | ||||||||||||
| 49 | 0 | ▶ | $r->{input_rows} //= []; | ||||||||||
|
Condition0%0/2
$r->{"input_rows"} //= []
MC/DC0%0/1
$r->{"input_rows"} //= []
$r->{"input_rows"}
Truth table0%0/2
A
A= $r->{"input_rows"}
| |||||||||||||
| 50 | |||||||||||||
| 51 | 0 | push @{ $r->{input_rows} }, $r->{input_row}; | |||||||||||
| 52 | }, | ||||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | after_close_input_files => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 55 | 0 | my $r = shift; | |||||||||||
| 56 | |||||||||||||
| 57 | # we do the actual shuffling here after collecting all the rows | ||||||||||||
| 58 | |||||||||||||
| 59 | 0 | require List::Util; | |||||||||||
| 60 | 0 | my $shuffled_rows = [List::Util::shuffle(@{$r->{input_rows}})]; | |||||||||||
| 61 | |||||||||||||
| 62 | 0 | for my $row (@$shuffled_rows) { | |||||||||||
| 63 | 0 | $r->{code_print_row}->($row); | |||||||||||
| 64 | } | ||||||||||||
| 65 | }, | ||||||||||||
| 66 | 1 | ); | |||||||||||
| 67 | |||||||||||||
| 68 | 1 | 1; | |||||||||||
| 69 | # ABSTRACT: Shuffle CSV rows | ||||||||||||
| 70 | |||||||||||||
| 71 | __END__ | ||||||||||||