perl命令行參數(shù)內(nèi)建數(shù)組@ARGV淺析

字號:


    當(dāng)perl腳本運(yùn)行時,從命令行上傳遞給它的參數(shù)存儲在內(nèi)建數(shù)組@ARGV中,@ARGV是PERL默認(rèn)用來接收參數(shù)的數(shù)組,可以有多個參數(shù),$ARGV[0]是表示接收到的第一個參數(shù),$ARGV[1]表示第二個。
    使用方法為:
    復(fù)制代碼 代碼如下:perl my.pl $ARGV[0] $ARGV[1]
    看一個具體例子:
    比如文件1的內(nèi)容:
    復(fù)制代碼 代碼如下:1320238
    1320239
    1320239
    1320238
    1320238
    1320238
    1320235
    1320237
    文件2的內(nèi)容:
    復(fù)制代碼 代碼如下: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的內(nèi)容讀取出來,然后讀取文件二的內(nèi)容,在讀取文件2的內(nèi)容的時候,文件2的最后一列需要包含在上文件1內(nèi)。
    復(fù)制代碼 代碼如下:[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";