| 1 | package App::CSVUtils::csv_transpose; | ||||||||||||
| 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_transpose', | ||||||||||||
| 16 | summary => 'Transpose a CSV', | ||||||||||||
| 17 | description => <<'MARKDOWN', | ||||||||||||
| 18 | |||||||||||||
| 19 | This utility transpose a CSV file: rows will become fields and vice versa. | ||||||||||||
| 20 | |||||||||||||
| 21 | Example: | ||||||||||||
| 22 | |||||||||||||
| 23 | # input.csv | ||||||||||||
| 24 | name,age | ||||||||||||
| 25 | andi,17 | ||||||||||||
| 26 | budi,22 | ||||||||||||
| 27 | chandra,19 | ||||||||||||
| 28 | dudi,20 | ||||||||||||
| 29 | |||||||||||||
| 30 | % csv-transpose input.csv | ||||||||||||
| 31 | row0,row1,row2,row3,row4 | ||||||||||||
| 32 | name,andi,budi,chandra,dudi | ||||||||||||
| 33 | age,17,22,19,22 | ||||||||||||
| 34 | |||||||||||||
| 35 | % csv-transpose input.csv --no-output-header | ||||||||||||
| 36 | name,andi,budi,chandra,dudi | ||||||||||||
| 37 | age,17,22,19,22 | ||||||||||||
| 38 | |||||||||||||
| 39 | MARKDOWN | ||||||||||||
| 40 | |||||||||||||
| 41 | add_args => { | ||||||||||||
| 42 | }, | ||||||||||||
| 43 | tags => ['category:munging'], | ||||||||||||
| 44 | |||||||||||||
| 45 | examples => [ | ||||||||||||
| 46 | { | ||||||||||||
| 47 | summary => 'Transpose a CSV', | ||||||||||||
| 48 | argv => ['file.csv'], | ||||||||||||
| 49 | test => 0, | ||||||||||||
| 50 | 'x.doc.show_result' => 0, | ||||||||||||
| 51 | }, | ||||||||||||
| 52 | ], | ||||||||||||
| 53 | |||||||||||||
| 54 | 0 | ▶ | on_input_header_row => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||||
| 55 | 0 | my $r = shift; | |||||||||||
| 56 | |||||||||||||
| 57 | # this is a key we add ourselves | ||||||||||||
| 58 | 0 | $r->{transposed_rows} = []; | |||||||||||
| 59 | |||||||||||||
| 60 | 0 | for my $j (0 .. $#{ $r->{input_fields} }) { | |||||||||||
| 61 | 0 | ▶ | $r->{transposed_rows}[ $j ] //= []; | ||||||||||
|
Condition0%0/2
$r->{"transposed_rows"}[$j] //= []
MC/DC0%0/1
$r->{"transposed_rows"}[$j] //= []
$r->{"transposed_rows"}[$j]
Truth table0%0/2
A
A= $r->{"transposed_rows"}[$j]
| |||||||||||||
| 62 | 0 | $r->{transposed_rows}[ $j ][0] = $r->{input_fields}[$j]; | |||||||||||
| 63 | } | ||||||||||||
| 64 | |||||||||||||
| 65 | 0 | $r->{output_fields} = ["row1"]; | |||||||||||
| 66 | }, | ||||||||||||
| 67 | |||||||||||||
| 68 | 0 | ▶ | on_input_data_row => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 69 | 0 | my $r = shift; | |||||||||||
| 70 | |||||||||||||
| 71 | 0 | my $i = $r->{input_rownum}; | |||||||||||
| 72 | 0 | push @{ $r->{output_fields} }, "row$i"; | |||||||||||
| 73 | 0 | for my $j (0 .. $#{ $r->{input_row} }) { | |||||||||||
| 74 | $r->{transposed_rows}[ $j ][ $i-1 ] = | ||||||||||||
| 75 | 0 | $r->{input_row}[$j]; | |||||||||||
| 76 | } | ||||||||||||
| 77 | }, | ||||||||||||
| 78 | |||||||||||||
| 79 | 0 | ▶ | after_close_input_files => sub { | ||||||||||
|
Subroutine
__ANON__: not called CC=2 SCAR=17.9
| |||||||||||||
| 80 | 0 | my $r = shift; | |||||||||||
| 81 | |||||||||||||
| 82 | 0 | for my $row (@{ $r->{transposed_rows} }) { | |||||||||||
| 83 | 0 | $r->{code_print_row}->($row); | |||||||||||
| 84 | } | ||||||||||||
| 85 | }, | ||||||||||||
| 86 | 1 | ); | |||||||||||
| 87 | |||||||||||||
| 88 | 1 | 1; | |||||||||||
| 89 | # ABSTRACT: Transpose a CSV | ||||||||||||
| 90 | |||||||||||||
| 91 | __END__ | ||||||||||||