[問題] 遞迴呼叫

看板Perl作者 (打字男)時間16年前 (2008/12/07 12:02), 編輯推噓0(002)
留言2則, 1人參與, 最新討論串1/3 (看更多)
小的在寫一個 印出所有字串中字元排列組合 的程式 在遞迴呼叫的時候出了一點問題 不知道有無大大可以幫我看一下, 再印出 mylist 的地方只有第一次印的出來, 不知道為什麼? 是我參數傳遞有錯嗎? 謝謝 main 呼叫: &perm(0,$size-1,\@array); sub perm{ my ($head, $tail, $list) = @_ ; my $indexj; print "head-tail: $head, $tail\n"; print "mylist @$list\n" ; if( $head == $tail ){ #stop condition print "result: @$list\n" ; } else{ for($indexj = $head ; $indexj <= $tail ; $indexj++ ){ ($list[head], $list[indexj]) = ($list[indexj], $list[head]) ; &perm($head+1,$tail,\@list); ($list[head], $list[indexj]) = ($list[indexj], $list[head]) ; } } } -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 218.169.88.182

12/07 22:16, , 1F
喔喔 找到問題了 list沒加@, 陣列subscript 沒加$
12/07 22:16, 1F

12/07 22:16, , 2F
太腦殘了, 謝謝下面的大大提供範例 感謝
12/07 22:16, 2F
文章代碼(AID): #19EqhSmw (Perl)
討論串 (同標題文章)
文章代碼(AID): #19EqhSmw (Perl)