http://www.perlmonks.org?node_id=25393


in reply to Writing in reverse

Here is another way to do it using chop.
#!/usr/bin/perl -w use IO::File; my $fh1 = new IO::File; my $fh2 = new IO::File; $fh1->open("< file1.txt") or die "can't open file1.txt $!"; $fh2->open("< file2.txt") or die "can't open file2.txt $!"; my $file1 = <$fh1>; my $file2 = <$fh2>; print "Merging:\n"; print "\t$file1\n"; print "\t$file2\n"; while(length $file1){ my $couplet = chop($file1).chop($file2); print "$couplet "; }

Replies are listed 'Best First'.
RE: Re: Writing in reverse
by Anonymous Monk on Aug 02, 2000 at 01:50 UTC
    Eak, Thankx for the answer, What if I want the arrays to merge in backward order? like first file is : abcde, second file is fghij, I want ej di ch bg af. By the way it only worked for one array only rather than whole file . It didn't read whole file for some reason. Thankx.
      never mind..I got it
        Actually, it didn't work. When I use @file1 , it still reads the first lines and keeps give me blank lines and never stops...
RE: Re: Writing in reverse
by Adam (Vicar) on Aug 02, 2000 at 01:04 UTC
    You fell into the same trap as chromatic in that you only read the first line of the file. Either localize $/ to undef or use my @file1 = <$fh1>

    Other then that though, this is very slick and you use a much better way of scoping the file handle.