Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Perl command line for table join functionality

by Laurent_R (Canon)
on Apr 30, 2014 at 19:22 UTC ( [id://1084542]=note: print w/replies, xml ) Need Help??


in reply to Perl command line for table join functionality

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.

Replies are listed 'Best First'.
Re^2: Perl command line for table join functionality
by Anonymous Monk on May 07, 2014 at 11:28 UTC

    How about this ?

    perl -ne 'print if ($seen{$_} .= @ARGV) =~ /10$/' fileA fileB >output

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1084542]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (5)
As of 2024-03-28 18:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found