Link to home
Start Free TrialLog in
Avatar of steph84
steph84

asked on

position in a file.

I'd like to know how to manage position in a file.
Ex : say I've this file :
hello
hi
everybody
BEGIN
some string
another one
END
yes
no
foo
BEGIN2
exactly
two lines
END2
bar
foo

Suppose I use IN and OUT as filehandles for input and output.
I want to read my file till BEGIN then print all lines till END
then browse till BEGIN2 and then print exactly the 3 following lines
then stop browsing

What's the most efficient and "perl style" manner to do that???
How can I modify the position of <IN> in my file??
ASKER CERTIFIED SOLUTION
Avatar of shchuka
shchuka

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ozo
The last string will be $infile[@infile-1]
 or $infile[$#infile] or $infile[-1]
If you know the position you want in the file, you can seek to  it.
otherwise, you could say something like:
open(IN,"inputfile.txt") or die "Can't open inputfile.txt: $!";
while( <IN> ){
   print if /^BEGIN/ .. /^END/;
}
Avatar of steph84
steph84

ASKER

Ok, In fact I was looking for something like the seek command. Could you confirm that $. is the current line number??

Is there a special variable to print current line ($_ doesn't work!) If I have :
while (<IN>) {
print "the_line";
}
does just print; work????


$. is the number of times <> was executed on the current file handle,
which will be the current line if you've been reading line by line.
 while( <IN> ){
   print;
 }
and
 while( <IN> ){
   print $_;
 }
are equivalent

Avatar of steph84

ASKER

Ok, I grade the first answer (I've a lot of point and this answer was not bad, even if i knew that!). Then I'll ask a blank question you'll be able to answer.
Thanks.