A Brief Analysis of the Built-in Array @ARGV of Perl Command Line Parameters

Time:2019-5-21

When the Perl script runs, the parameters passed to it from the command line are stored in the built-in array @ARGV, @ARGV is the default array used by PERL to receive parameters, which can have multiple parameters, $ARGV [0] is the first parameter received, and $ARGV [1] is the second.
The method of use is as follows:

Copy codeThe code is as follows:
perl   my.pl $ARGV[0]  $ARGV[1]

Look at a specific example:
For example, the contents of file 1:

Copy codeThe code is as follows:
1320238
1320239
1320239
1320238
1320238
1320238
1320235
1320237

Content of document 2:

Copy codeThe code is as follows:
102 5709072117805887 4001 1301854
102 5709072117807510 4001 1320292
102 5709072117838653 4001 1301857
102 5709072117814280 4001 1305832
102 5709072117839397 4001 1310673
102 5709072117839335 4001 1311270

I want to read the contents of file 1 first, and then read the contents of file 2. When reading the contents of file 2, the last column of file 2 needs to be included in file 1 above.

Copy codeThe code is as follows:
[[email protected] ~]$ perl  ex.pl 1.txt 2.txt
[[email protected] ~]$ 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”;