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.
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.