Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

read certain lines in a file

Status
Not open for further replies.

unclescrooge

Programmer
Joined
Feb 5, 2007
Messages
4
Location
ZA
Hi

I need some help, I have created a file with a perl script with contents in it how do I print each and every second line out of the file?

Thanks
 
please post the code you have so far. Or look into using the $. variable (input record line number) and the modulus '%' operator, or using Tie::File (or reading the file into an array: not recommended for really big files), and some initialization condition:

Code:
[black][b]for[/b][/black] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue] [black]=[/black] [fuchsia]0[/fuchsia][black],[/black] [blue]$i[/blue] [black]<[/black] [blue]@array[/blue][black],[/black] [blue]$i[/blue][black]+=[/black][fuchsia]2[/fuchsia][red])[/red][gray][i]#for even numbers[/i][/gray]
   [black][b]print[/b][/black] [red]"[/red][purple][blue]$array[/blue][[blue]$i[/blue]][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]

[black][b]for[/b][/black] [red]([/red][black][b]my[/b][/black] [blue]$i[/blue] [black]=[/black] [fuchsia]1[/fuchsia][black],[/black] [blue]$i[/blue] [black]<[/black] [blue]@array[/blue][black],[/black] [blue]$i[/blue][black]+=[/black][fuchsia]2[/fuchsia][red])[/red][gray][i]#for odd numbers[/i][/gray]
   [black][b]print[/b][/black] [red]"[/red][purple][blue]$array[/blue][[blue]$i[/blue]][purple][b]\n[/b][/purple][/purple][red]"[/red][red];[/red]
[red]}[/red]



- Kevin, perl coder unexceptional! [wiggle]
 
Hi

My code is as follows:

open OUTPUT,">text.txt";

for (@list)
{
print OUTPUT ( (split)[3, 8], "\n")

}
close OUTPUT;

open INPUT,"<text.txt";

@list=<INPUT>;

close INPUT;


print "Every second line is:\n";
for ($list = 0, $list < @list, $list+=2)
{
print "@list[$list]\n";

}
 
I'm sorry, my code was not correct. In the initialization part the commas should be semi-colons:

Code:
for ($list = 0[red];[/red] $list < @list[red];[/red] $list+=2)



- Kevin, perl coder unexceptional! [wiggle]
 
Hi

If you look at my code where I did the second loop to print certain lines. That works great but now my text file is open how do I re-write or safe the changes into the text file as print OUTPUT .. is not working
 
I don't understand your question.

- Kevin, perl coder unexceptional! [wiggle]
 
unclescrooge said:
If you look at my code where I did the second loop to print certain lines. That works great but now my text file is open how do I re-write or safe the changes into the text file as print OUTPUT .. is not working

At that point in your code OUTPUT is closed and INPUT is closed!
To re-write to your text file, open it again.
NB if you use:
open OUTPUT, ">text.txt";
it will overwrite text.txt
If you use
open OUTPUT, ">>text.txt";
it will append to the end of the file.


I hope that helps.

Mike
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top