By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,563 Members | 1,058 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,563 IT Pros & Developers. It's quick & easy.

read multi lines of a file in perl

P: 1
Hi all,
I have to read severals line of a document in perl. I wrote a program but with that am able to read only lines by line. eg if i have a file in which contents are:
Expand|Select|Wrap|Line Numbers
  1. process jkdgf
  2. jdk
  3. jhfk
  4. end process
  5. process jhdk
  6. jfka
  7. end process
  8.  
I have to read data betwenn process and end process. But am not able to.
my code is
Expand|Select|Wrap|Line Numbers
  1. while(<WORDLIST>)
  2. {
  3.     if (/process/)    #check for process
  4.     {
  5.             print "$_";
  6.  
  7.         if (/end process/)
  8.     {
  9.     last;
  10.     }
  11. }}
  12. close (WORDLIST);
  13.  
am just able to read only
Expand|Select|Wrap|Line Numbers
  1. process jkdgf
  2. end process
  3.  
as output....
i want output to be as
Expand|Select|Wrap|Line Numbers
  1. jdk
  2. jhfk
  3. jfka
  4.  
Many Thanks in advance..
Nov 5 '08 #1
Share this Question
Share on Google+
3 Replies


numberwhun
Expert Mod 2.5K+
P: 3,503
Well, that is because you are looking for the terms "process" and "end process" and printing them when found. You are not telling it to do anything else. This script is essentially doing exactly what you have told it to do.

Try using something like this inside of the while loop:

Expand|Select|Wrap|Line Numbers
  1. unless ( $_ ~= /(?:process|end process)/) {
  2.      print $_;
  3. }
  4.  
Mind you, that is completely untested.

Regards,

Jeff
Nov 5 '08 #2

KevinADC
Expert 2.5K+
P: 4,059
Well, that is because you are looking for the terms "process" and "end process" and printing them when found. You are not telling it to do anything else. This script is essentially doing exactly what you have told it to do.

Try using something like this inside of the while loop:

Expand|Select|Wrap|Line Numbers
  1. unless ( $_ ~= /(?:process|end process)/) {
  2.      print $_;
  3. }
  4.  
Mind you, that is completely untested.

Regards,

Jeff
New operator '~='? I think that is the mysterious and undocumented "maybe equals" operator. ;)

The answer to this question is found in the Perl FAQs. The second and or third FAQ on this page should help:

http://perldoc.perl.org/perlfaq6.html


.
Nov 5 '08 #3

numberwhun
Expert Mod 2.5K+
P: 3,503
New operator '~='? I think that is the mysterious and undocumented "maybe equals" operator. ;)

The answer to this question is found in the Perl FAQs. The second and or third FAQ on this page should help:

http://perldoc.perl.org/perlfaq6.html


.
LOL!! Ok, so I had it backwards, it should be =~. Sorry for the confusion.

**I must revisit the FAQs one of these days**
Nov 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.