in reply to match two files

Here is simple solution:

#!/usr/bin/perl my @tmp1 = map { chomp; [ split /\s+/, $_, -1 ] } `cat tmp01`; # (*) my %tmp2 = map { chomp; my @a = split /\s+/, $_, -1; ($a[0], \@a) } `cat tmp02`; print join "\t" @$_, @{ $tmp2{$$_[0]} }, "\n" for @tmp1; # (*) instead of `cat tmp01` it is more safe to: #use Path::Tiny; #path("tmp01")->lines_utf8({chomp => 1});

Replies are listed 'Best First'.
Re^2: match two files
by GrandFather (Saint) on Dec 10, 2020 at 21:01 UTC
    Here is simple solution:

    "simple" by what measure? Fewer statements does not equate to simple.

    If using Path::Tiny is safer why do you demonstrate less safe code?

    Optimising for fewest key strokes only makes sense transmitting to Pluto or beyond

      I prefer to read whole files, and print them joined. All at once. I don't like algorithmic C-style flow: open file, read line by line, process line, print line...

      One can use whichever solution is better for his situation -- `cat file` or Path::Tiny... I don't know how to slurp whole file other simpler way.

Re^2: match two files
by afoken (Chancellor) on Dec 11, 2020 at 15:42 UTC

    Here is simple solution:

    #!/usr/bin/perl my @tmp1 = map { chomp; [ split /\s+/, $_, -1 ] } `cat tmp01`; # (*) my %tmp2 = map { chomp; my @a = split /\s+/, $_, -1; ($a[0], \@a) } `cat tmp02`; print join "\t" @$_, @{ $tmp2{$$_[0]} }, "\n" for @tmp1; # (*) instead of `cat tmp01` it is more safe to: #use Path::Tiny; #path("tmp01")->lines_utf8({chomp => 1});

    Congratulations, you have won not just one, but two Useless Use of Cat Awards!

    Further more, you have prepared two shell injection vulnerabilities (The problem of "the" default shell), you artifically restricted the "solution" to Unix systems (Windows and many other systems have no cat), and you are limiting the sum of the input file sizes to significantly less than available RAM and swap by not reading line-by-line, but instead reading both input files all at once.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)