当前位置 博文首页 > perl命令行参数内建数组@ARGV浅析

    perl命令行参数内建数组@ARGV浅析

    作者:admin 时间:2021-02-02 18:14

    当perl脚本运行时,从命令行上传递给它的参数存储在内建数组@ARGV中,@ARGV是PERL默认用来接收参数的数组,可以有多个参数,$ARGV[0]是表示接收到的第一个参数,$ARGV[1]表示第二个。
    使用方法为:

    复制代码 代码如下:
    perl   my.pl $ARGV[0]  $ARGV[1]

    看一个具体例子:
    比如文件1的内容:
    复制代码 代码如下:
    1320238
    1320239
    1320239
    1320238
    1320238
    1320238
    1320235
    1320237

    文件2的内容:

    复制代码 代码如下:
    102 5709072117805887 4001 1301854
    102 5709072117807510 4001 1320292
    102 5709072117838653 4001 1301857
    102 5709072117814280 4001 1305832
    102 5709072117839397 4001 1310673
    102 5709072117839335 4001 1311270

    我想先把文件1的内容读取出来,然后读取文件二的内容,在读取文件2的内容的时候,文件2的最后一列需要包含在上文件1内。

    复制代码 代码如下:
    [root@localhost ~]$ perl  ex.pl 1.txt 2.txt
    [root@localhost ~]$ cat ex.pl
    #!/usr/bin/perl
    use strict;

    open(ONE,"$ARGV[0]") or die $!;
    open(TWO,"$ARGV[1]") or die $!;

    my %hash;
    while (<TWO>) {
        chomp;
        my @line=split;
        my $column4=$line[3];
        $hash{$column4}=$_;
    }

    while (<ONE>) {
         chomp;
         print $hash{$_} if defined $hash{$_};
       
    }

    print"\n";

    js