This post is part of a series on Mohammad Anwar’s excellent Weekly Challenge, where hackers submit solutions in Perl, Raku, or any other language, to two different challenges every week. (It’s a lot of fun, if you’re into that sort of thing.)
The second task this week is simple: given a (simplified) comma-separated-value (CSV) file, transpose its rows and columns. For example:
\(
\begin{bmatrix}
A & B & C & D \\
1 & 2 & 3 & 4 \\
w & x & y & z
\end{bmatrix}^T
\Rightarrow
\begin{bmatrix}
A & 1 & w \\
B & 2 & x \\
C & 3 & y \\
D & 4 & z
\end{bmatrix}
\)
The challenge task does not actually refer to the input as CSV, so I’m using that term loosely, with simplified parsing to match the input specification. If more compliant parsing is needed, one could use the usual Text::CSV module in Perl, or ports in Raku.
There are a couple of ways to transpose files, which I’ll explore in Raku and Perl.
Continue reading “PWC 110 › Transpose CSV File”