Re: [問題] Algorithm::Diff的使用

看板Perl作者 (St. Kevin)時間15年前 (2010/03/13 01:20), 編輯推噓1(100)
留言1則, 1人參與, 最新討論串2/3 (看更多)
#!/usr/local/bin/perl -w use Algorithm::Diff qw(diff sdiff LCS traverse_sequences traverse_balanced); @str1 = split(//,"abcehjlmnp"); @str2 = split(//,"bcdefjklmrst"); @lcs = LCS(\@str1, \@str2); @diff = diff(\@str1, \@str2); print "LCS : @lcs\n"; pretty_print_diff(@diff); sub pretty_print_diff { my @diff = @_; print "[\n"; for my $array_ref (@diff) { print " [\n"; for (@$array_ref) { print " [ ", join(", ", map { "'$_'" } @$_), " ],\n"; } print " ],\n"; } print "]\n"; } ---------- 不知這樣子的結果可不可以接受? Output: [ [ [ '-', '0', 'a' ], ], [ [ '+', '2', 'd' ], ], [ [ '-', '4', 'h' ], [ '+', '4', 'f' ], ], [ [ '+', '6', 'k' ], ], [ [ '-', '8', 'n' ], [ '+', '9', 'r' ], [ '-', '9', 'p' ], [ '+', '10', 's' ], [ '+', '11', 't' ], ], ] ※ 引述《marsline (火星線上)》之銘言: : 想請教一下,我使用了diff function,但是我不知要如何取得如網頁上的範例結果。 : 我試了print @diffs or $diffs都是回傳給我記憶體位址。先謝謝大家的幫忙。 : (抱歉我還是個新手…) : 下列是我的程式碼: : #!/usr/local/bin/perl -w : use Algorithm::Diff qw(diff sdiff LCS traverse_sequences traverse_balanced); : @str1 = split(//,"abcehjlmnp"); : @str2 = split(//,"bcdefjklmrst"); : @lcs = LCS(\@str1, \@str2); : @diff = diff(\@str1, \@str2); : print "LCS : @lcs\n"; : print "Diff: @diff\n"; : 回傳結果: : LCS : b c e j l m : Diff: ARRAY(0x8230ac8) ARRAY(0x8230b34) ARRAY(0x8230c48) ARRAY(0x8230cb4) : ARRAY(0x8230d2c) : 想要的結果:(如網頁上的範例結果) : [ : [ [ '-', 0, 'a' ] ], : [ [ '+', 2, 'd' ] ], : [ [ '-', 4, 'h' ] , : [ '+', 4, 'f' ] ], : [ [ '+', 6, 'k' ] ], : [ [ '-', 8, 'n' ], : [ '-', 9, 'p' ], : [ '+', 9, 'r' ], : [ '+', 10, 's' ], : [ '+', 11, 't' ], : ] : ] : 參考網頁: : http://search.cpan.org/~nedkonz/Algorithm-Diff-1.15/lib/Algorithm/Diff.pm#diff -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.214.6

03/13 18:02, , 1F
可以,非常感謝您的幫忙
03/13 18:02, 1F
文章代碼(AID): #1BcdVh_w (Perl)
文章代碼(AID): #1BcdVh_w (Perl)