| 1 | package App::CSVUtils::csv_shuf_fields; | ||
| 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_fields', | ||
| 19 | summary => 'Shuffle CSV fields', | ||
| 20 | description => <<'MARKDOWN', | ||
| 21 | |||
| 22 | This utility shuffles the order of fields in the CSV. Example input CSV: | ||
| 23 | |||
| 24 | a,b,c,d | ||
| 25 | 1,2,3,4 | ||
| 26 | 5,6,7,8 | ||
| 27 | |||
| 28 | Example output CSV: | ||
| 29 | |||
| 30 | d,a,c,b | ||
| 31 | 4,1,3,2 | ||
| 32 | 8,5,7,6 | ||
| 33 | |||
| 34 | MARKDOWN | ||
| 35 | |||
| 36 | add_args => { | ||
| 37 | }, | ||
| 38 | |||
| 39 | tags => ['category:sorting'], | ||
| 40 | |||
| 41 | 0 | ▶ | on_input_header_row => sub { |
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||
| 42 | 0 | my $r = shift; | |
| 43 | |||
| 44 | 0 | require List::Util; | |
| 45 | 0 | my @shuffled_indices = List::Util::shuffle(0 .. $#{$r->{input_fields}}); | |
| 46 | |||
| 47 | 0 | $r->{output_fields} = [map {$r->{input_fields}[$_]} @shuffled_indices]; | |
| 48 | 0 | $r->{output_fields_idx_array} = \@shuffled_indices; # this is a key we add to stash | |
| 49 | }, | ||
| 50 | |||
| 51 | 0 | ▶ | on_input_data_row => sub { |
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||
| 52 | 0 | my $r = shift; | |
| 53 | |||
| 54 | 0 | my $row = []; | |
| 55 | 0 | for my $j (@{ $r->{output_fields_idx_array} }) { | |
| 56 | 0 | push @$row, $r->{input_row}[$j]; | |
| 57 | } | ||
| 58 | 0 | $r->{code_print_row}->($row); | |
| 59 | }, | ||
| 60 | 1 | ); | |
| 61 | |||
| 62 | 1 | 1; | |
| 63 | # ABSTRACT: Shuffle CSV fields | ||
| 64 | |||
| 65 | __END__ | ||