Perl- creating a matrix from a 3 column file

Dear all,
I'm new in perl scripting and I'm trying to creating a matrix from a 3 column file sorting data in a particular manner. In the final matrix I need to have the first column "IDs" on the header of the columns and the second column values on the header of each row. And the value fo the third column in each right position. As follows:
My input look like:

aa 3 5
cc 9 11
bb 15 7
aa 12 5
cc 18 11
bb 6 7

And I need to have an output like this:

aa bb cc
3 5       
6   7
9     11
12 5    
15   7
18     11

Hope to be clear...
thank you for any help!!

Like this?

perl -ane '$val1{$F[0]}{$F[1]}=$F[2];$val2{$F[1]}{$F[0]}=$F[2];
END{
$"="\t";
@h=sort keys %val1;
print " ", $", "@h\n";
for $y (sort {$a<=>$b} keys %val2) {
 print "$y",$";
 for $x (@h) {
  print "$val1{$x}{$y}",$";
 }
 print "\n";
}
}' file

I'm trying it...but doesn't work properly :frowning: