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

FileHandle - searching single word from a txt file

P: 16
hello guys

i set this code , and i would like to know if its possible set "quotes" on the searched word.

Thank You

Expand|Select|Wrap|Line Numbers
  1. #word searching program within a txt file
  2. #!C:/perl/bin/perl.exe
  3. start:
  4. #Handle Area - file opening
  5. open FILE, "c:/perl/notice.txt" or die "failed to open notice.txt $!\n";
  6. @read=<FILE>;
  7. close FILE;
  8. chomp @read;    
  10. #Searching Area
  11.     print "Search a word\?\n";
  12.         $choose=<STDIN>; chomp $choose;
  13. unless($choose =~/y|Y|[Y,y]es/){
  14.         print "invalid \n"; 
  15.         goto start;
  16. }
  18.        print "Type the word\n";
  19.                $choose=<STDIN>; chomp $choose;
  20.                my @result= grep(/$choose/,@read); 
  21.        print "Sorry, this word was not found" unless(@result);  
  22.                foreach(@result) { 
  23.             print "results: ''$_''";               
  25.         }
May 26 '09 #1
Share this Question
Share on Google+
5 Replies

Expert 100+
P: 971
If you want the quotes to print just escape them. Or you can do the following.
Expand|Select|Wrap|Line Numbers
  1. my $word = 'perl';
  2. print qq{The word you searched was "$word"};
May 26 '09 #2

P: 16
sorry i didnt get what you meant with these codes,
May 26 '09 #3

Expert Mod 2.5K+
P: 3,503
Try putting the code that eWish has provided into your script and see if it does what you want. The way he has written it, it will print the quotes around one of the words.

You will probably need to read up on the qq as to what it does so you understand.


May 27 '09 #4

P: 16
i see, the results quoted the word i typed on STDIN, but if i would like to keep the whole text line still on the results but with the quote on the exact word i entered on STDIN, ... could it be possible??

Thanks for patience
May 27 '09 #5

Expert 100+
P: 971
Yes. Simply use my example from above and replace the text and variables with those from your code.

May 28 '09 #6

Post your reply

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