Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Re: to interchange line no of file & print

by Utilitarian (Vicar)
on Jun 28, 2011 at 09:40 UTC ( [id://911714]=note: print w/replies, xml ) Need Help??


in reply to to interchange line no of file & print

Wow, you don't learn do you.
Do you mean that you need to merge two files by printing alternate lines from each?

To do that you could

  • set a $data_left flag to 3
  • open both files, checking your opens succeeded
  • while the $data_left flag is true
    • if you can read from the first file and $data_left%2 = 1
      • print out the line you read
    • if however you couldn't read from the first file and $data_left%2 = 1
      • set the $data_left flag to $data_left - 1
    • if you can read from the second file and $data_left >= 2.
      • print out the line you read
    • if however you couldn't read from the second file and $data_left >= 2
      • set the $data_left flag to $data_left - 2
  • close the files

This assumes you want to continue adding data after one of the files is empty and that you don't want to insert blank lines as placeholders for the empty file

print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2024-04-18 00:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found