Connecting Tech Pros Worldwide Forums | Help | Site Map

read multi lines of a file in perl

Newbie
 
Join Date: Nov 2008
Posts: 1
#1: Nov 5 '08
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..

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,573
#2: Nov 5 '08

re: read multi lines of a file in perl


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
KevinADC's Avatar
Expert
 
Join Date: Jan 2007
Location: Southern California USA
Posts: 4,091
#3: Nov 5 '08

re: read multi lines of a file in perl


Quote:

Originally Posted by numberwhun

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


.
numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,573
#4: Nov 5 '08

re: read multi lines of a file in perl


Quote:

Originally Posted by KevinADC

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**
Reply