FileHandle - searching single word from a txt file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Sevla
    New Member
    • May 2009
    • 16

    FileHandle - searching single word from a txt file

    hello guys

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

    Thank You


    Code:
    #word searching program within a txt file
    #!C:/perl/bin/perl.exe
    start:
    #Handle Area - file opening
    open FILE, "c:/perl/notice.txt" or die "failed to open notice.txt $!\n";
    @read=<FILE>;
    close FILE;
    chomp @read;	
    
    #Searching Area
    	print "Search a word\?\n";
    		$choose=<STDIN>; chomp $choose;
    unless($choose =~/y|Y|[Y,y]es/){
    		print "invalid \n"; 
    		goto start;
    }
    
           print "Type the word\n";
                   $choose=<STDIN>; chomp $choose;
                   my @result= grep(/$choose/,@read); 
           print "Sorry, this word was not found" unless(@result);  
           		foreach(@result) { 
            	print "results: ''$_''";               
                          
            }
    Last edited by eWish; May 26 '09, 07:11 PM. Reason: Please use the [code][/code] tags.
  • eWish
    Recognized Expert Contributor
    • Jul 2007
    • 973

    #2
    If you want the quotes to print just escape them. Or you can do the following.
    Code:
    my $word = 'perl';
    print qq{The word you searched was "$word"};
    --Kevin

    Comment

    • Sevla
      New Member
      • May 2009
      • 16

      #3
      sorry i didnt get what you meant with these codes,

      Comment

      • numberwhun
        Recognized Expert Moderator Specialist
        • May 2007
        • 3467

        #4
        Originally posted by Sevla
        sorry i didnt get what you meant with these codes,
        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.

        Regards,

        Jeff

        Comment

        • Sevla
          New Member
          • May 2009
          • 16

          #5
          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

          Comment

          • eWish
            Recognized Expert Contributor
            • Jul 2007
            • 973

            #6
            Yes. Simply use my example from above and replace the text and variables with those from your code.

            --Kevin

            Comment

            Working...