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??
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 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
which will be the current line if you've been reading line by line.
while( <IN> ){
print;
}
and
while( <IN> ){
print $_;
}
are equivalent
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.
Thanks.
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/;
}