Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris

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!!


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

Replies are listed 'Best First'.
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
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

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://965536]
Approved by halfcountplus
Front-paged by halfcountplus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (11)
As of 2017-09-19 21:00 GMT
Find Nodes?
    Voting Booth?
    During the recent solar eclipse, I:

    Results (229 votes). Check out past polls.