manojkumar vajram wrote:
> Plz. help me . I am new to perl
> I want to accept a pattern from screen
> and search the pattern in my one or two text data base
> and print the details
> 
> The pattern from screen may consist spaces and special
> char. line / - etc.
> I use redhat linux 8.0
> I have coded like below but fetched no result
> code------------------------------------------>
> system( "echo -en 'Enter Pattern :\c'");
> $pat = <STDIN>;
> open (FILEONE,"file1");
> while(<FILEONE>){
> if($pat=~$_){print $_;}
> close(FILEONE);
> open (FILETWO,"file2");
> while(<FILETWO>){
> if($pat=~$_){print $_;)
> close(FILETWO);
> 
> end of code----------------------------------->

Main problem is you need to chomp() your pattern text, and you have your
regex comparisons backwards.

The lines:

   if($pat=~$_){print $_;}

Should be:

   if ($_ =~ $pat) { print $_; }

Or, the simpler

   print if /$pat/;

Or, what you *really* want:

   print if /$pat/o;

Other issues:

1. Add "use strict;" at the top of all your programs. "use warnings;" is
recommended as well.

2. Don't use system("echo") to print a prompt. Use Perl's print() function.

3. Always check the return value from system calls like open().

4. Use whitepsace and indententation.

5. Learn statement modifiers for writing clearer code. Perl's not C. Instead
of:

   if (COND) { EXPR; }

you can write the clearer:

   EXPR if COND;

6. Consider a loop to process the two files rather than duplicating code.

7. Since $pat never changes in your program adding /o to the regex will
speed up the program. see perldoc perlre for more details.

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
<http://learn.perl.org/> <http://learn.perl.org/first-response>


Reply via email to