Solved

position in a file.

Posted on 1998-08-03
5
149 Views
Last Modified: 2010-03-05
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??
0
Comment
Question by:steph84
  • 2
  • 2
5 Comments
 
LVL 2

Accepted Solution

by:
shchuka earned 50 total points
ID: 1208985
Why do you need to modify the position in your input file?  Why can't you just read it line by line until you see BEGIN, then read and print lines until you see END and so on?

If the file is not too big, you can read the entier file in a string array and then do whatever you want with the elements of the array.  I.e. something like this:

open(IN,"inputfile.txt");
@infile = <IN>;
close(IN);

Now $infile[0] will contain the first string from your file, $infile[1] the second one and so on.  The last string will be $infile[@infile].
0
 
LVL 84

Expert Comment

by:ozo
ID: 1208986
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/;
}
0
 

Author Comment

by:steph84
ID: 1208987
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????


0
 
LVL 84

Expert Comment

by:ozo
ID: 1208988
$. 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

0
 

Author Comment

by:steph84
ID: 1208989
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.
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now