CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
to interchange line no of file & print.

 



harshmane
Novice

Jun 27, 2011, 11:26 PM

Post #1 of 15 (1235 views)
to interchange line no of file & print. Can't Post

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.


harshmane
Novice

Jun 28, 2011, 1:08 AM

Post #2 of 15 (1231 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

use warnings;
use strict;
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];
}


(This post was edited by harshmane on Jun 28, 2011, 3:46 AM)


rovf
Veteran

Jun 28, 2011, 3:29 AM

Post #3 of 15 (1222 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

A few notes to your posting:

(1) What is a "interchange line number"?

(2) Always write

use strict; use warnings FATAL => qw(all);

on top of your program, in particular if you are a beginner, because this not only saves you a lot of effort, but other readers might refuse to comment your code if you don't make your program strict.

(3) As a beginner, also write

use diagnostics;

on top of your program.

(4) Since $i is an integer, $i%2 can only take the values 0 and 1, so instead of two different 'if' statements, you better use a single 'if-else' statement.


harshmane
Novice

Jun 28, 2011, 3:48 AM

Post #4 of 15 (1220 views)
Re: [rovf] to interchange line no of file & print. [In reply to] Can't Post

interchange means.
normal order of lines is
1
2
3
4
5
6
after interchanging alternately orderof lines is
2
1
4
3
6
5


rovf
Veteran

Jun 28, 2011, 4:03 AM

Post #5 of 15 (1219 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

I see. This is a special case of a more general problem, which has been discussed here:

http://perlguru.com/gforum.cgi?post=57404

Look in particular at my response further down the thread. What you in effect want to do is to read within a loop two lines of the file.


harshmane
Novice

Jun 28, 2011, 11:22 AM

Post #6 of 15 (1212 views)
Re: [rovf] to interchange line no of file & print. [In reply to] Can't Post

i have written program my sir ask me to use only 1 file handler and no use of while loop also so i use goto keyword Mad wich i dont want 2 use ....but dats yway i have done it & file is havin 6 lines.

Code
use warnings; 
use strict;
$i=0;

open (SRC,"hi.txt");
open(DEST,">h3.txt");



$time =3;


hi : $lin =<SRC>;
$lin1=<SRC>;


if($i==0)
{
print DEST $lin1;
$i++;
}

if($i==1)
{
print DEST $lin;

$i--;
$time--;
}
if($time>0)
{
goto hi;
}


if any one know other method to do samething in other way.


rovf
Veteran

Jun 28, 2011, 11:32 AM

Post #7 of 15 (1209 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

(1) I guess you mean "file handle" and not "file handler". Correct? These are very different things, so please be careful with your wording.

(2) You arfe using 2 file handles, not one. However, since you are dealing with two files, you WILL need 2 file handles. If your "Sir" asks you to use only one, you have two choices: Either kick him at the place just below his spine (this would be the best), or first read the whole file into memory, close it, open the same file handle for writing, write the change file, and after this kick your Sir into the buttocks at least twice.

(3) I don't think it is a good idea to use "goto".

(4) You don't need to count the lines. It is trivial to implement tis for a file ahving any number of lines.

Otherwise, your usage of "goto" is technically correct, just that nobody being more sane than the zombies depicted in "The Night Of The Living Dead" would program it that way. A "while" loop would be absolutely fine here.

(5) (Now this is the most important observation for your problem): In every iteration of your loop, you are reading 2 lines, but you are writing at most one. This means that h3.txt will contain at most 2 lines (one for $i equals 0, one for $i equals 1). You will have to write 2 lines in each iteration, lest your output file won't have the same number of lines than your input file.


FishMonger
Veteran / Moderator

Jun 28, 2011, 11:45 AM

Post #8 of 15 (1208 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

Don't use the 'goto' function!

If your instructor told you to not use a while loop, then use a do/until loop.

Since this is a homework assignment, I won't post a complete solution, but I will give you a big hint.

Code
do { 
....
....
} until eof SRC;



harshmane
Novice

Jun 28, 2011, 11:47 AM

Post #9 of 15 (1207 views)
Post deleted by harshmane [In reply to]

 


harshmane
Novice

Jun 28, 2011, 10:39 PM

Post #10 of 15 (1199 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

i use

Code
 do { 
} until eof SRC

its workin but is there any other way to do it w/o using any loop.


harshmane
Novice

Jun 28, 2011, 11:34 PM

Post #11 of 15 (1196 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

i also tried out $/ it working fine for such operation.


rovf
Veteran

Jun 29, 2011, 12:29 AM

Post #12 of 15 (1195 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post


Quote
is there any other way to do it w/o using any loop.


In theory, yes. Any loop can replaced by suitable recursive functions (though sometimes available stack space might limit its application). This would be possible (though not advisable) in your case too, though as a teacher, this is not something I would ask a beginner to do.


harshmane
Novice

Jun 29, 2011, 1:21 AM

Post #13 of 15 (1194 views)
Re: [rovf] to interchange line no of file & print. [In reply to] Can't Post

would you please give me any example of it.


(This post was edited by harshmane on Jun 29, 2011, 1:21 AM)


rovf
Veteran

Jun 29, 2011, 1:51 AM

Post #14 of 15 (1191 views)
Re: [harshmane] to interchange line no of file & print. [In reply to] Can't Post

For example, the loop


Code
my @arr=qw(how nice is Okinawa in winter); 
foreach (@arr) {
print(uc($_),"\n");
}


could also be written as


Code
sub px {  
if(@_) {
print(uc($_[0],"\n");
shift;
px(@_);
}
}
my @arr=qw(how nice is Okinawa in winter);
px(@arr);


(Note: Code above NOT tested).

For completeness, it should be added that (depending on your definition of the word "loop"), the Perl functions 'map' and 'grep' could also be used for replacing a loop by a non-loop.


FishMonger
Veteran / Moderator

Jun 29, 2011, 6:23 AM

Post #15 of 15 (1186 views)
Re: [rovf] to interchange line no of file & print. [In reply to] Can't Post

And depending on how loose you want to be on the definition of a loop, a recursive sub could also be thought of as a loop.

Without knowing the exact requirements the instructor gave, it's hard to give any suggestion that doesn't require some form of looping.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives