in reply to Converting Arrays into Matrix
producesmy @TwoD = ( ['A','B','C'], ['A','D','C'], ['A','B','C'] ); my ( $slotH, %slotH ) = (-1,()); my @result = (); for ( my $i1 = 0; $i1 <= $#TwoD; $i1++ ) { for ( my $i2 = 0; $i2 <= $#{ $TwoD[ $i1 ]}; $i2++ ) { my $v = $TwoD[ $i1 ][ $i2 ]; defined( $slotH{ $v } ) or $slotH{ $v } = ++$slotH; $result[$i1][$slotH{ $v }] = $v; } for ( my $i2 = 0; $i2 <= $slotH; $i2++ ) { $i2 and print "\t"; print ( $result[$i1][$i2] || '' ); } print "\n"; }
Update: actually my version needs its output rotating 90 degrees to get exactly the same output, but the technique is similar for doing that, resulting in minor change to the above script.A B C A C D A B C
One world, one people
|
|---|