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本身必须是排序完之后的。
  • 相关阅读:
    angular 路由请求js文件
    WeX5的简单介绍及UI的简单讲解
    JAVA 反射
    JAVA 线程
    JAVA 远程通讯机制
    用Java实现几种常见的排序算法
    自动完成
    Springmvc和poi3.9导出excel并弹出下载框
    Windows Server 搭建企业无线认证(NPS搭建)
    Windows Server 搭建企业无线认证(Radius认证方案)
  • 原文地址:https://www.cnblogs.com/xiaopengren/p/perl.html
Copyright © 2011-2022 走看看