Your program does a lot of useless things. No point to store your files into arrays and then process the arrays, process the files directly. Also a lot of useless parens. This is a possible rewrite, about twice shorter (untested, I don't have data, and not sure it does exactly what you want, some of your code seemed a bit strange to me, I might have changed it the wrong way, not having data does not help):
use strict; use warnings; my %hash1; open my $FH, "<", $ARGV[0] or die "Could not open input file $ARGV[0] +$!"; while (chomp (my $line = <$FH>)) { my $file1_element = (split /\t/, $line)[5]; $hash1{$file1_element} = $_; } close $FH; open my $FH2, "<", $ARGV[1] or die "Could not open input file $ARGV[1] + $!"; while (chomp (my $line = <$FH2>)) { my $file2_element = (split /\t/, $line)[5]; print $_ . "\t$hash1{$file2_element}\n" if exists $hash1{$file2_el +ement}; }
Now, if you want to do this at the command line, you can just do this:
$ perl -e ' use strict; > use warnings; > my %hash1; > open my $FH> , "<",m $yA RG%V[0] or die "Could not open input file $ +1ARGV[0;] $!"; > open my $FH, "<", $ARGV[0] or die "Could not open input file $ARGV[0 +] $!"; > while (chomp (my $line = <$FH>)) { > my $file1_element = (split /\t/, $line)[5]; > $hash1{$file1_element} = $_; > } > close $FH; > open my $FH2, "<", $ARGV[1] or die "Could not open input file $ARGV[ +1] $!"; > while (chomp (my $line = <$FH2>)) { > my $file2_element = (split /\t/, $line)[5]; > print $_ . "\t$hash1{$file2_element}\n" if exists $hash1{$file2_elem +ent}; > } ' file1.txt file2.txt
But why should you want to do this? Why not having a real program in a file? Command line instructions are good for very short code, not for this. I could probably reduce the code by another half on the command line, perhaps even a bit more than that, but this is still too long for a one-liner of a pure prompt command.

Edit: Modified slightly the code, as some things coming from the OP code did not seem right to me.


In reply to Re: Perl command line for table join functionality by Laurent_R
in thread Perl command line for table join functionality by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.