Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

position in a file.

Posted on 1998-08-03
5
Medium Priority
?
158 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 200 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 85

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 85

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

[Webinar] Cloud Security

In this webinar you will learn:

-Why existing firewall and DMZ architectures are not suited for securing cloud applications
-How to make your enterprise “Cloud Ready”, and fix your aging DMZ architecture
-How to transform your enterprise and become a Cloud Enabler

Question has a verified solution.

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

I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
Six Sigma Control Plans

916 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