•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 456,272 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,435 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our Perl advertiser: Programming Forums
Views: 461 | Replies: 4
![]() |
•
•
Join Date: Sep 2008
Posts: 3
Reputation:
Rep Power: 0
Solved Threads: 0
Where should i look for this answer:
Hi,
If you don't know the answer if you could please point me in the right direction
it would be appreciated. looking to search for more then 1 term at a time
with user interaction as shown in the script below. I have tried || but only
end up with everything being displayed. The current script displays the row
directly in front of the search term but I'm looking for it to display mutiple
search terms. Is this possible or does my entire script need to be re-written?
something along the lines of:
Search for: Name: || Number:
or
Search for: Name:, Number:
with the end results being something like:
John Wayne 225-3455-1234
Smith Berry 225-3143-2341
Hi,
If you don't know the answer if you could please point me in the right direction
it would be appreciated. looking to search for more then 1 term at a time
with user interaction as shown in the script below. I have tried || but only
end up with everything being displayed. The current script displays the row
directly in front of the search term but I'm looking for it to display mutiple
search terms. Is this possible or does my entire script need to be re-written?
something along the lines of:
Search for: Name: || Number:
or
Search for: Name:, Number:
with the end results being something like:
John Wayne 225-3455-1234
Smith Berry 225-3143-2341
perl Syntax (Toggle Plain Text)
************************************************************ use Tie::File; use Fcntl 'O_RDONLY'; $inputFile = 'source.txt'; $outputFile = 'output.txt'; print 'Enter 1 to print to screen, 2 to print to a file: '; chomp($outputOpt = <STDIN>); #chomp removes newline from input string print 'Search for: '; chomp(my $searchPattern = <STDIN>); tie @lines, 'Tie::File', $inputFile, mode => O_RDONLY or die "Can't open $inputFile for reading: $!\n"; if (@results = grep(/$searchPattern/,@lines)){ if ($outputOpt == 1){ print join("This box will close in 10 seconds\n",@results); } elsif ($outputOpt == 2){ open(OUTPUT, ">$outputFile") or die "Can't open $outputFile for writing: $!\n"; print OUTPUT join("\n",@results); close(OUTPUT); print "Everything is in a file called $outputFile\n"; } } else { print "No match & this box will close in 10 seconds\n"; } $num = 10; while($num--){ sleep(1); }
Last edited by cscgal : Sep 20th, 2008 at 12:22 am. Reason: Added code tags
•
•
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation:
Rep Power: 6
Solved Threads: 68
Use ~=m/ ... , do not use grep.
grep is OK in this situation because you have to search the entire file. grep uses "$_ =~ m/pattern/" internally to search for the pattern.
When you say search for multiple things in the file, how does the user input those multiple things? You have to tell the user something like:
Enter multiple search terms seperated by a space:
then your script gets the search terms and uses split() to create a list of those search terms and searches for them sequentially, or you could actually compile a regexp or a search pattern from the input after split()ing it into the serperate search terms.
When you say search for multiple things in the file, how does the user input those multiple things? You have to tell the user something like:
Enter multiple search terms seperated by a space:
then your script gets the search terms and uses split() to create a list of those search terms and searches for them sequentially, or you could actually compile a regexp or a search pattern from the input after split()ing it into the serperate search terms.
![]() |
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Need Client Machine's System information (JavaScript / DHTML / AJAX)
- The Definitive Guide to which Forum Software Information (Growing an Online Community)
- Is it possible to get information about client's machine ? (ASP)
- Requesting information about trojan file jquery.corner[1].js (Viruses, Spyware and other Nasties)
- can i find the lost memory area ??? (Windows NT / 2000 / XP / 2003)
- cannot find hardware (Windows NT / 2000 / XP / 2003)
- Compiling VB6 crash - now with more information (Visual Basic 4 / 5 / 6)
- Some useful information. (Viruses, Spyware and other Nasties)
- Viruses found need some information (Viruses, Spyware and other Nasties)
Other Threads in the Perl Forum
- Previous Thread: Extract everything between a <span> tag ignoring any othe tag , newline , comma in it
- Next Thread: Is it possible to pass a subroutine to another subroutine?



Linear Mode