Click here to Skip to main content
15,891,828 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

I am a newbie to Perl and have written a code to search for the word "PERFORM" at the start of a line in a file and the corresponding line should be printed in another file. Also I need to store the string after PERFORM in an array
PERL
use strict;  
use warnings; 

open FILE1, "<Test3.txt" or die "Cannot open File1.txt!";
open FILE2, ">File2.txt" or die "Cannot open File2.txt!";


while (my $string=<FILE1>) {
	
	if($string =~ m/^PERFORM\s*/)
	{
	print (FILE2 "$string\n");
	}
	

}
close FILE2;
close FILE1;


But I get output of lines starting with "*" and "END-PERFORM" also.

Thanks in advance.
Posted
Updated 25-Jul-12 21:13pm
v2
Comments
Chandrasekharan P 26-Jul-12 4:12am    
We tried the same program. It works for me. Can you send me some of the lines in your Test3.txt file.
Faez Shingeri 26-Jul-12 4:35am    
Here is my Test3.txt
<pre lang="text">
* *****************************************************
* 12/18/02 BPV188 A71921 REL 01/03 *
* REPLACED FCOPYBK WITH F2COPYBK TO *
* IMPROVE THE FILE ACCESS PERFORM ANCE *
* *****************************************************
data to be ignored
data to be ignored
data to be ignored

PERFORM op1 op2 op3
data to be ignored
data to be ignored

PERFORM op4 op5 op6

PERFORM op7 op8 op9

PERFORM op0 op0 op0

data to be ignored
data to be ignored

END-PERFORM
END-PERFORM
</pre>


Output file is as below:

<pre lang="text">
PERFORM op4 op5 op6

PERFORM op7 op8 op9

PERFORM op0 op0 op0

END-PERFORM

END-PERFORM
</pre>
Chandrasekharan P 26-Jul-12 6:42am    
I tried it with the data you gave and below is the output that i am getting

PERFORM op1 op2 op3

PERFORM op4 op5 op6

i did not put PERFORM op7 op8 and op9 in the text file.
Faez Shingeri 26-Jul-12 6:47am    
:-o Are you sure that you aren't getting this line too..?
* IMPROVE THE FILE ACCESS PERFORM ANCE * //Note the space after PERFORM

Could you please share your exact code.?

Thanks
Faez Shingeri 31-Jul-12 0:45am    
if($string =~ m/^PERFORM\b/) solved the issue.. :)

Thanks,

1 solution

You can begin by reading some regex documentation. The trailing "\s*" does nothing. It is interpreted as zero or more occurances of a whitespace character.

Here is a simple to understand regex review ...

http://www.cs.tut.fi/~jkorpela/perl/regexp.html[^]

As for more specific trouble.. it would help if you included lines that match from your file which you think should not.
 
Share this answer
 
Comments
Faez Shingeri 31-Jul-12 0:45am    
Thanks a lot David. The link was brilliant.!
if($string =~ m/^PERFORM\b/) solved the issue..:-D

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900