比较经典的一种对比文件的方式。。看了这段代码有点感悟。
但没有写出来,回头整理看看。。
#! /usr/bin/perluse strict; use warnings; my $src_lines_1_ref = get_lines_from_file('1.txt'); my $src_lines_2_ref = get_lines_from_file('2.txt'); my @dst_lines = grep { my $line = $_; grep $_ eq $line,@$src_lines_1_ref; } @$src_lines_2_ref; write_lines_to_file('3.txt',\@dst_lines); sub get_lines_from_file { my $file = shift || ""; my @lines; open my $file,"<$file" or dIE "Cannot open $file: $!"; while (<$file>) { chomp; next if /^\s*$/ #删除空行 s/^\s*//; # 注释掉行头的空格 s/\s*$//; # 注释掉行尾的空格 push @lines,$_; } close $file; return \@lines; } sub write_lines_to_file { my $file = shift || ""; my $lines_ref = shift || ""; open my $file,">$file" or dIE "Cannot open $file: $!"; for (@$lines_ref) { print $file $_."\n"; } close $file; }总结
以上是内存溢出为你收集整理的Perl文件内容对比全部内容,希望文章能够帮你解决Perl文件内容对比所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)