Analysis of Perl command line parameter built-in array @ argv

Time:2022-1-19

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 array used by Perl to receive parameters by default. There can be multiple parameters, $argv [0] represents the first received parameter and $argv [1] represents the second.
Usage:

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


Take a specific example:
For example, the contents of file 1:

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

 

Contents 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.

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”;

 

Recommended Today

04 project architecture – componentization – detailed explanation

1、 Background With the gradual expansion of the project, there are more and more business functions, more and more code and more developers. During this process, have you ever had the following troubles? There are many and complex project modules. Compile for 5 minutes or even 10 minutes at a time? Too slow to bear? […]