perl命令行参数内建数组@ARGV浅析
当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的内容:
102570907211780588740011301854 102570907211780751040011320292 102570907211783865340011301857 102570907211781428040011305832 102570907211783939740011310673 102570907211783933540011311270
我想先把文件1的内容读取出来,然后读取文件二的内容,在读取文件2的内容的时候,文件2的最后一列需要包含在上文件1内。
[root@localhost~]$perl ex.pl1.txt2.txt [root@localhost~]$catex.pl #!/usr/bin/perl usestrict;
open(ONE,"$ARGV[0]")ordie$!; open(TWO,"$ARGV[1]")ordie$!;
my%hash; while(<TWO>){ chomp; my@line=split; my$column4=$line[3]; $hash{$column4}=$_; }
while(<ONE>){ chomp; print$hash{$_}ifdefined$hash{$_}; }
print"\n";