当前位置: 移动技术网 > IT编程>网页制作>Perl > perl命令行参数内建数组@ARGV浅析

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

2017年12月08日  | 移动技术网IT编程  | 我要评论

当perl脚本运行时,从命令行上传递给它的参数存储在内建数组@argv中,@argv是perl默认用来接收参数的数组,可以有多个参数,$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";

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网