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

Re: Writing in reverse

by eak (Monk)
on Aug 01, 2000 at 06:42 UTC ( #25393=note: print w/replies, xml ) Need Help??

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.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (6)
As of 2022-01-22 21:12 GMT
Find Nodes?
    Voting Booth?
    In 2022, my preferred method to securely store passwords is:

    Results (63 votes). Check out past polls.