Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How to supply two input files at command line

by ZWcarp (Beadle)
on Apr 17, 2012 at 17:27 UTC ( #965536=perlquestion: print w/ replies, xml ) Need Help??
ZWcarp has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, thank you for your time. I am wondering about the proper syntax for inputing files at the command line and sending them to separate hashes. perl -walne '$h{$F[0]}.="$F[1]";END{print "$_:\t" . $h{$_} for sort keys%h}' file1.txt .

Now lets say I want to read in a second file as a second hash in the same execution... what is the syntax to tell perl that file1 and file2 should go to separate hashes?

perl -walne '$h{$F[0]}.="$F[1]" Now here is where I want to read in file2.txt into %h2 ;END{$_=......... ' file1.txt file2.txt

Any ideas on how to do this? Thanks a ton!!

UPDATE

how about something simpler... file one is a list, and I want to print out every line in file 2 that matches the current element in the list. In a script you would do this by making a nested foreach loop or you could do it by making a hash and then checking the hash.. How would one do this at command line (specifically dealing with 2 files ) ? Something like this where the file 2 lines only are piped in to the matching part:

 perl -F: -walne'$h{$F[0]}.="$F[1] "; END{#Line from file2 here#=~/$h{$_}/&&print"$h{$_}"for keys%h}' file1.txt file2.txt

Comment on How to supply two input files at command line
Select or Download Code
Re: How to supply to input files at command line
by brx (Pilgrim) on Apr 17, 2012 at 17:53 UTC
    Take a look to eof
    In a while (<>) loop, eof or eof(ARGV) can be used to detect the end of each file, whereas eof() will detect the end of the very last file only.

    Update: try this not tested version
     perl -walne '$;="\t";$i||=0;$h{$i,$F[0]}.="$F[1]" } continue { $i++ if eof; END{print "$_:\t" . $h{$_} for sort keys %h}' file1.txt file2.txt

    Seems OK

    $ perl -MO=Deparse -walne '$;="\t";$i||=0;$h{$i,$F[0]}.="$F[1]" } cont +inue { $i++ if eof; END{print "$_:\t" . $h{$_} for sort keys %h}' fil +e1.txt file2.txt BEGIN { $^W = 1; } BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = <ARGV>)) { chomp $_; our(@F) = split(' ', $_, 0); $; = "\t"; $i ||= 0; $h{join $;, $i, $F[0]} .= "$F[1]"; } continue { ++$i if eof; } sub END { print "${_}:\t" . $h{$_} foreach (sort keys %h); } -e syntax OK
Re: How to supply to input files at command line
by jwkrahn (Monsignor) on Apr 17, 2012 at 19:54 UTC

    $ARGV is the name of the current file being processed so you could do something like this:

    perl -walne '$h{$ARGV}{$F[0]}.="$F[1]";END{ print ...}' file1.txt file +2.txt

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965536]
Approved by halfcountplus
Front-paged by halfcountplus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2014-08-29 01:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (275 votes), past polls