01. Algorithm:Diff 模块

  • 代码地址
    • https://github.com/ReneNyffenegger/PerlModules/blob/master/Algorithm/Diff/script.pl
  • 代码作用:对比seq_a和seq_b字符串的不同,并且将每次对比出的不同输出。
  • qw() qw 是 quote word 的缩写,用于创建一个单引号括起的单词列表。它提供了一种简便的方式来创建包含空格分隔的单词的数组。是一个用于创建单引号字符串列表的语法糖
    • my @array = qw(word1 word2 word3 …);
    • 其中,word1、word2、word3 等是以空格分隔的单词。qw 返回一个包含这些单词的数组。这对于创建简单的字符串数组非常方便,尤其是在不希望使用引号进行转义的情况下。
    • 注意,qw 不会进行变量插值,它只是简单地将每个空格分隔的单词转化为数组的元素。如果需要进行变量插值或其他更复杂的字符串处理,可以使用双引号或者其他适当的字符串操作。
    • 以下代码等价
    • my @colors = qw(red green blue yellow);
    • my @colors = (‘red’, ‘green’, ‘blue’, ‘yellow’);
  • my $diff = Algorithm::Diff->new(@seq_a, @seq_b);传递给Diff模块的是 @seq_a和@seq_b的引用
  • (join “-”, @items_a) 将数组 @items_a 中的元素用下划线 “” 连接起来形成一个字符串。具体来说,join 函数接受两个参数,第一个参数是连接的字符串(这里是下划线 “”),第二个参数是要连接的数组(这里是 @items_a)。
  • (01…33) 创建了一个包含数字 1 到 33 的数组。
    map { sprintf(“%02d”, $) } 对数组中的每个元素执行操作。sprintf(“%02d”, $) 的作用是将数字格式化为两位的整数,不足两位的部分用零填充。
    结果是一个新的数组 @array,其中每个元素都是原始数组中的元素经过格式化后的字符串。
#!/usr/bin/perl -w

use strict;
use warnings;

my @array = map { sprintf("%02d",$_) } (01..33);
print @array, "\n";
foreach (@array) {
    print $_, " ";
}
print "\n";

my $join_array = (join "_", @array);
print $join_array,"\n";

  • 其输出结果
010203040506070809101112131415161718192021222324252627282930313233
01 02 03 04 05 06 07 08 09 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 
01_02_03_04_05_06_07_08_09_10_11_12_13_14_15_16_17_18_19_20_21_22_23_24_25_26_27_28_29_30_31_32_33
use warnings;
use strict;

use Algorithm::Diff;


my @seq_a = qw(       a x b y c z p d q 1 2 3 4 ! 5 6 7);
my @seq_b = qw( a b c a x b y c z       1 2 3 4 ? 5 6 7);

#
#  Turn seq_a into seq_b
#
#  This is the array that will contain
#  the same values as @seq_b:

my @seq_B = ();


#
#  new computes the smallest set of additions and deletions
#  necessary to turn the first sequence into the second.
#
my $diff = Algorithm::Diff->new(\@seq_a, \@seq_b);

while (my $hunk =   #  A hunk represents a contiguous section
                    #  of items which should be added, deleted
                    #  replaced, or left unchanged.
                    #  ---------------------------------------

           $diff -> Next()

      ) {

     print "\n";

  #  ----------------------------------

     print "$hunk: $hunk ";

     print "Diff: ", $diff->Diff(), " ";

     print "Same " if $diff->Same();

     print "\n";

     my @items_a = $diff -> Items(1);
     my @items_b = $diff -> Items(2);

     push @seq_B, @items_b;

     print "  A: " . (join "-", @items_a) . "\n";
     print "  B: " . (join "-", @items_b) . "\n";
}

print "------------------------\n";

print ((join "-", @seq_b),"\n");
print ((join "-", @seq_B),"\n");
  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值