Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!usc!isi.edu!sondeen From: sondeen@isi.edu (Jeff Sondeen) Newsgroups: comp.lang.perl Subject: Problem permutating perl program... help please! Message-ID: <16402@venera.isi.edu> Date: 16 Jan 91 22:25:24 GMT Reply-To: sondeen@venera.isi.edu (Jeff Sondeen) Distribution: comp Organization: Information Sciences Institute, Univ. of So. California Lines: 48 Could someone explain why the following permutating perl program doesn't work? The idea is to generate all permutations of a list 1..$siz (for example, input of 3 should yield (1,2,3) (1,3,2) (2,3,1) (2,1,3) (3,1,2) and (3,2,1), but it doesn't. It just does (1,2,3), as follows perl permute.p 3 len:3 i:1 ins: 1 2 3 outs: len:2 i:1 ins: 2 3 outs: 1 len:1 i:1 ins: 3 outs: 1 2 1 2 3 len:1 i:1 ins: 3 outs: 1 2 len:2 i:2 ins: 3 2 outs: 1 len:3 i:1 ins: 2 3 1 outs: It might be that somehow the local variable $i is getting clobbered (acting like a global???) perl program follows: ------------------------------------------------------ $siz = @ARGV[0]; @terms = (1..$siz); do permute($siz,@terms,()); exit; sub permute { local($len,@outs) = @_; local(@ins) = splice(@outs,0,$len); local($i,$out); if ($len > 0) { for $i (1..$len) { print "len:$len i:$i ins: @ins outs: @outs\n"; $out = shift(@ins); do permute($len-1,@ins,@outs,$out); push(@ins,$out); print "len:$len i:$i ins: @ins outs: @outs\n"; } } else { print "@outs\n"; } } -- /jeff sondeen@isi.edu "engineers were discouraged from bringing problems to the attention of their supervisors" -- John Magnus, final report, Hubble Space Telescope investigation