1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
#!/usr/bin/perl
#
# Task 2: "Transpose File
#
# You are given a text file.
#
# Write a script to transpose the contents of the given file.
#
# Input File
#
# name,age,sex
# Mohammad,45,m
# Joe,20,m
# Julie,35,f
# Cristina,10,f
#
# Output:
#
# name,Mohammad,Joe,Julie,Cristina
# age,45,20,35,10
# sex,m,m,f,f
# "
#
# My notes: simple to state. Basically read into 2-D array and transpose
# array.
#
use strict;
use warnings;
use feature 'say';
use Function::Parameters;
use Text::CSV;
#use Data::Dumper;
#
# my @t = transpose(@a);
# Transpose @a into @t.
#
fun transpose( @a )
{
my $cols = @{$a[0]};
my @result;
foreach my $r (0..$#a)
{
foreach my $c (0..$cols-1)
{
$result[$c][$r] = $a[$r][$c];
}
}
return @result;
}
die "Usage: tranposecsv\n" unless @ARGV==0;
my @a; # 2-d array
my $csv = Text::CSV->new ( { binary => 1 } )
|| die "Cannot use CSV: ".Text::CSV->error_diag ();
while( <> )
{
my $status = $csv->parse($_);
die "bad line $_\n" unless $status;
my @f = $csv->fields();
push @a, \@f;
}
#die Dumper \@a;
my @t = transpose(@a);
foreach my $row (@t)
{
$csv->print( \*STDOUT, $row);
print "\n";
}
|