zoukankan      html  css  js  c++  java
  • perl 脚本学习-----两个文件排序之后输入到一个文件

    举个例子:

    file1是这样的:
    11111111111111
    22222222222222
    55555555555555
    66666666666666

    file2是这样的:
    33333333333333
    44444444444444
    77777777777777
    88888888888888

    而我想得到的结果file3是:
    11111111111111
    22222222222222
    33333333333333
    44444444444444
    55555555555555
    66666666666666
    77777777777777
    88888888888888

    程序举例:

    open IN1,'<',$file1;
    open IN2,'<',$file2;
    open OUT,'>',$file_out;
     
    my $line1 = <IN1>;
    my $line2 = <IN2>;
     
    while (1) {
       
      # do this, when a file reaches its end
      if (!defined $line1) {
        # file1 ended, write lasting parts of file2
        print OUT $line2;
        while (<IN2>) { print OUT }
        last;
      }
       
      if (!defined $line2) {
        # file2 ended, vise versa
        print OUT $line1;
        while (<IN1>) { print OUT }
        last;
      }
     
      # the main compare
      if ($line1<$line2) {
        print OUT $line1;
        $line1 = <IN1>;
      }
      elsif ($line2<$line1) {
        print OUT $line2;
        $line2 = <IN2>;
      }
      else {
        die "EXCEPTION: two identical lines: <$line1> and <$line2>";
      }
    }
    程序局限:
    file1 和 file2本身必须是排序完之后的。
  • 相关阅读:
    Spring Boot 内嵌Tomcat的端口号的修改
    仅显示INPUT下边框
    2015面试记三
    2015面试记二
    2015面试记一
    最近工作学习心得体会
    Tomcat批处理文件小结
    启动Tomcat一闪而过——分析及解决过程
    WIN7安装及配置JDK
    Firefox下载文件时中文名乱码问题
  • 原文地址:https://www.cnblogs.com/xiaopengren/p/perl.html
Copyright © 2011-2022 走看看