Re: [問題] get the same elements

看板Perl作者 (笑哈哈)時間21年前 (2003/03/06 23:12), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串2/4 (看更多)
※ 引述《easy1 ( ~_~ !)》之銘言: : @a = qw/a b c d e f/; : @b = qw/a c g h f d b/; : how can I get the same elements in @a and @b ? 好像沒有直接的function能做到這樣的事 不過你可以這樣做 @a = qw(a b c d e f); @b = qw(a c g h f d b); @aa = sort @a; @bb = sort @b; $i = $j = 0; while ($i < $#aa && $j < $#bb) { $i++ if ($aa[$i] lt $bb[$j]); $j++ if ($aa[$i] gt $bb[$j]); if ($aa[$i] eq $bb[$j]) { print $aa[$i] . "\n"; $i++; $j++; } } -- ※ 發信站: 批踢踢實業坊(ptt.csie.ntu.edu.tw) ◆ From: 140.112.30.183
文章代碼(AID): #-PsLH4f (Perl)
文章代碼(AID): #-PsLH4f (Perl)