Wednesday, April 13, 2011

rotating the array in perl

#Rotating the alphabet the three letters
@letters = ('A' .. 'Z');
for ($i = 1; $i <= 3; $i++) {
unshift(@letters, pop(@letters));
}
print @letters, "\n";


#rotating the alphabet in the opposite direction
@letters = ('A' .. 'Z');
for ($i = 1; $i <= 3; $i++) {
push(@letters, shift(@letters));
}
print @letters, "\n";

No comments: