SoFunction
Updated on 2025-04-07

A brief analysis of built-in array of perl command line parameters @ARGV

When the perl script is run, the parameters passed to it from the command line are stored in the built-in array @ARGV. @ARGV is the array used by PERL to receive parameters by default. There can be multiple parameters. $ARGV[0] is the first parameter received, and $ARGV[1] is the second one.
How to use it is:

Copy the codeThe code is as follows:
perl   $ARGV[0]  $ARGV[1]

See a specific example:
For example, the content of file 1:
Copy the codeThe code is as follows:
1320238
1320239
1320239
1320238
1320238
1320238
1320235
1320237

Contents of file 2:

Copy the 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 content of file 1 first, and then read the content of file 2. When reading the content of file 2, the last column of file 2 needs to be included in the previous file 1.

Copy the codeThe code is as follows:
[root@localhost ~]$ perl 
[root@localhost ~]$ cat
#!/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";