Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid

Re: reading two files in parallel

by LanX (Chancellor)
on May 02, 2013 at 19:32 UTC ( #1031798=note: print w/replies, xml ) Need Help??

in reply to reading two files in parallel

my ($a,$b); while( $a=<F1>, $b= <F2>, $a or $b) { ... }

reads till the end of the longer file. Changing to and limits to shorter one.

Cheers Rolf

( addicted to the Perl Programming Language)

UPDATE Please note

Since $a and $b are not chomped, no normal input line should ever be false and hence not necessarily tested with defined.

Better take care if you are using special filehandles allowed to return a simple 0 or null strings!!!



use strict; use warnings; use Data::Dump qw/pp/; open my $f1, "<", \ join "\n", 1..2; open my $f2, "<", \ join "\n", 1..5; while( defined (my $a=<$f1>) + defined (my $b=<$f2>) ) { $a .=""; $b .=""; chomp($a,$b); print "$a,$b\n"; }
1,1 2,2 ,3 ,4 ,5

In boolean context: + is like or, * is like and, just w/o short circuit.

Replies are listed 'Best First'.
Re^2: reading two files in parallel
by Laurent_R (Abbot) on May 02, 2013 at 22:06 UTC
    Beautiful idea, I did not think of this way of doing it, thank you, Rolf, it might make my module simpler... if I finally end up doing it.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1031798]
[Discipulus]: ridiculous! i'm leading the montly best section. oh tempora oh mores..
[Corion]: :-D
[Eily]: congrats Discipulus :)
[Eily]: (somehow I just typed "cong" and chrome proposed to complete the message :D )
[shmem]: Discipulus: well, a node with just one glaring truth on it is most easily upvoted ;-)
[Discipulus]: i'd like to be the vote fairy just one month.. ;=)

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (10)
As of 2017-04-26 11:50 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (473 votes). Check out past polls.