Solved

position in a file.

Posted on 1998-08-03
5
146 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
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…
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…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

920 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

15 Experts available now in Live!

Get 1:1 Help Now