def permute(in_str, prefix=""): if not len(in_str) : print(prefix) else: for i in range(0, len(in_str)): permute(in_str[:i] + in_str[i + 1:], prefix + in_str[i]) permute('12345') #### #!/usr/bin/perl -w use strict; my @array = 1..5; while (permute @array) sub permute { my @temp; while ( $#temp < $#_) { my $i; for ($i = $#temp; $i < $#_; $i++) { swap @_[$i] @_[$#temp]; permute; swap @_[$i] @_[$#temp]; } } print $_; } sub swap { my $k; my $l; my $temp; $temp = $k; $k = $l; $l = $temp; }