#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @a; my $x = 0; my $y = 0; #fill with 0 all the matrix with one loop(no need for two really :) ) $a[$_/8][$_%8]=0 for 0..63;#it's a 7x7 matrix :) #initialisation of matrix with provided data $a[0][0]=4; $a[0][1]=4; $a[0][2]=4; $a[0][3]=4; $a[0][4]=0; $a[0][5]=0; $a[0][6]=4; $a[0][7]=0; #matrix transpose calculated while(exists $a[$y][0] && $x<=$y) { # the last condition here is due to not # getting to lower triangle and transposing them # back getting exactly what we started with # wich we obviously don't want $x = 0; #reset $x for a new pass tghrough the current row while(exists $a[0][$x]) { ($a[$y][$x],$a[$x][$y]) = ($a[$x][$y],$a[$y][$x]) ; #swap the current element to its corresponding position in the transpose $x++;#increase the column }; $y++;#increase the row }