Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

to interchange line no of file & print

by harshmane (Initiate)
on Jun 28, 2011 at 06:28 UTC ( #911670=perlquestion: print w/ replies, xml ) Need Help??
harshmane has asked for the wisdom of the Perl Monks concerning the following question:

i have to open 1 file & copy in other file with interchange line no. for example : file1.txt hi how are you i am here file2.txt how are you hi here i am i solution of it i am having. i want other one.

Comment on to interchange line no of file & print
Re: to interchange line no of file & print
by choroba (Abbot) on Jun 28, 2011 at 07:14 UTC
    Please, use <c>...</c> tags around the file examples.
    How are we to give you another solution if we do not know which one you already have?
      please cheeck 1 solution of http://www.perlmonks.org/?node_id=911696
Re: to interchange line no of file & print
by Anonymous Monk on Jun 28, 2011 at 07:54 UTC
      use strict; use warnings; open (FF,"filename.txt"); # assume file is having 6 lines @array=<FF>; open (FF1,">file2write.txt"); for ($i=0;$i<=6;$i++) { if($i%2==0) { print FF1, @array[$i+1]; } if ($i%2==1) { print FF1,@array[$i-1]; }
        Every time you create a post here, you get a reminder to use code tags, it literally looks like
        Use:  <p> text here (a paragraph) </p>
        and:  <code> code here </code>
        to format your post; it's "PerlMonks-approved HTML"

        so follow the advice, and put your code in between code tags, its where code (and data) goes :)

        Now your code doesn't use lexical filehandles, which means you have not read perlintro . It also doesn't use strict or warnings, and in fact, it has syntax errors

        $ perl -c pm.911696.pl No comma allowed after filehandle at pm.911696.pl line 10
        So for the time being you should forget about whatever program you're trying to write and work through perlintro to get up to speed.

        Read also How do I post a question effectively?

        Read this if you want to cut your development time in half!

Re: to interchange line no of file & print
by Utilitarian (Vicar) on Jun 28, 2011 at 09:40 UTC
    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."
Re: to interchange line no of file & print
by Neighbour (Friar) on Jun 28, 2011 at 12:31 UTC
    If you're running in a *nix environment, there already exists a utility that does what you want: paste.
    man paste
    'nuff said :)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://911670]
Approved by davido
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (8)
As of 2014-08-02 02:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Who would be the most fun to work for?















    Results (53 votes), past polls