How to make the filhandle to go back to the previos line

S

sanjeeb

hey
i want to make the filhandle to go back to the previousline.

snippet:

while(<ARGV>)
{
if($line mathes)
{
#do this
}
else
{
# Go back the handle to the previous line or may be multiple line
#
}
}


i want to make a wrapper function where the argument will be the "How
mnay lines to be back" and the handle.

I know we can do by using a buffer but i dont want to use becuase the
line number argument can be big.

Is there any one have solution.

Regards
sanjeeb
 
M

Michele Dondi

i want to make the filhandle to go back to the previousline.

use a buffer instead!
I know we can do by using a buffer but i dont want to use becuase the
line number argument can be big.

perldoc -f tell
perldoc -f seek

and so on...


Michele
 
T

Tad McClellan

sanjeeb said:
i want to make the filhandle to go back to the previousline.


That depends on what the filehandle is connected to.

Some streams can be rewound and some can't.

i want to make a wrapper function where the argument will be the "How
mnay lines to be back" and the handle.

perldoc -f seek

perldoc -q filehandle

How can I make a filehandle local to a subroutine? How do I pass file-
handles between subroutines? How do I make an array of filehandles?
 
C

Charles DeRykus

sanjeeb said:
hey
i want to make the filhandle to go back to the previousline.

snippet:

while(<ARGV>)
{
if($line mathes)
{
#do this
}
else
{
# Go back the handle to the previous line or may be multiple line
#
}
}


i want to make a wrapper function where the argument will be the "How
mnay lines to be back" and the handle.

I know we can do by using a buffer but i dont want to use becuase the
line number argument can be big.

You could build an array of line start positions with seek/tell which
would be fastest (perldoc `seek` and `tell`). But, if a simpler, slower
solution is acceptable, look at `Tie::File` which enables you to keep an
array of the file's lines. So, jumping to another line is just a reset
of the array index.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top