zoukankan      html  css  js  c++  java
  • Perl文件内容对比

    比较经典的一种对比文件的方式。。看了这段代码有点感悟。

    但没有写出来,回头整理看看。。

    #! /usr/bin/perl
    
    use 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;
    }
  • 相关阅读:
    day63_django_html
    day62_django
    day20
    diango_自定义标签问题
    day64_django_orm
    day16_函数嵌套及对象
    day60_django
    pip 安装问题
    day13_文件操作
    文本溢出显示省略号(…) 小坦克
  • 原文地址:https://www.cnblogs.com/xiaoCon/p/2942105.html
Copyright © 2011-2022 走看看