Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

how can i search for data that uses + and - 2

Status
Not open for further replies.

MadMax007

Programmer
Sep 6, 2001
9
ES
I'm a begginer to PERL, but i can't seem to get round this problem.

In my prog. the user introduces a value to search for, and it all works fine, except that some of the data contains '+' and '-' , which causes confusion. The use of these signs in the data is essential. Here is a bit of the code. The user will introduce one of the 5 items in a line.
Code:
for($count=1;$count<=245;$count++) {
	$line[$count]=<DATA>;
}
$inlook=<STDIN>;
chop($inlook);
for($count=1;$count<=245;$count++) {
	if($line[$count]=~ /\b$inlook\b/i) {
		print(&quot;$line[$count]&quot;);
	}
}
__END__ 
K 2-5        014.9+06.4   17:54:25.0   -12:48:18    ES    
Na 1         018.0+20.1   17:12:51.3   -03:15:57    RS    
M 3-52       018.9+04.1   18:10:26.0   -10:29.1     ES    
M 3-55       021.7-00.6   18:33:14.7   -10:15:07    BR    
M 3-28       021.8-00.4   18:32:41.2   -10:05:48    Q     
M 1-57       022.1-02.4   18:40:20.2   -10:39:47    B     
MaC 1-13     022.5+01.0   18:28:35.1   -08:43:24    NC    
MA 2         022.5+04.8   18:15:13.0   -06:57.2     RS

Thanks

 
I'm not sure, but I think you need to use the q//, qq// or qr// functions, to have the variable quoted, rather than interpolated. Or I might be totally wrong...
 
MadMax007,

This is because the string used in the search pattern is first interpolated, and then checked for special metacharacters. The '+' and '-' have special meaning for regular expressions. See the 'perlre' manual pages for more information. You can use the '\Q' instruction, that turns off the metacharacter special handling, until a closing '\E' is seen:

For example the following code:

Code:
$str = &quot;a+b&quot;;

foreach ( qw( a+b aaaab ) ) {

    print &quot;(with metacharacters): '$str' matches $_\n&quot; if /$str/;
    print &quot;(without metacharacters): '$str' matches $_\n&quot; if /\Q$str/;

}

Returns:
Code:
(without metacharacters): 'a+b' matches a+b
(with metacharacters): 'a+b' matches aaaab

Cheers, NEIL

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top