in reply to How to create pivot tables via perl?
I had to do something similar in the recent past and here are two links that might be of help with the actual rotation/pivot portion of your solution:
using PDL->transpose on CSV file
Transpose a matrix of chars