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 IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

FileHandle - ''quoting" single word from searching within a file

Status
Not open for further replies.

kcire

Programmer
May 15, 2009
18
BR
hello guys

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

Thank You

#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: ''$_''";

}
 
Are you trying to put quotes so it's an exact match? Try looking for grep(/ $choose /,@read);
or myabe grep(/\s+$choose\s+/,@read);

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
I think they want to use quotes in the output. You must escape (\) double-quotes used in a double-quoted string:

Code:
 print "results: \"$_\"";

or use a quoting operator (in this case qq) that does the escaping for you:

Code:
 print qq{results: "$_"};


------------------------------------------
- Kevin, perl coder unexceptional! [wiggle]
 
i mean "quote" exactly the word you typed on the searching instead of the whole line!!

is it possible?


thank you
 
like this??

Code:
my @data = ('This is a test', 'This are not a test', 'blah blah not either');

my $search = 'is';

my @result = grep(/\b$search\b/, @data);

for my $line (@result) {
 $line =~ s/\b$search\b/"$search"/g;
 print "$line\n";
}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
[noevil]
Travis - Those who say it cannot be done are usually interrupted by someone else doing it; Give the wrong symptoms, get the wrong solutions;
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top