User Name Password Register
DaniWeb IT Discussion Community
All
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
Reply
Join Date: Sep 2008
Posts: 3
Reputation: 115cruiser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
115cruiser 115cruiser is offline Offline
Newbie Poster

where would i find this information?

  #1  
Sep 19th, 2008
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



  1. ************************************************************
  2. use Tie::File;
  3. use Fcntl 'O_RDONLY';
  4.  
  5. $inputFile = 'source.txt';
  6. $outputFile = 'output.txt';
  7.  
  8. print 'Enter 1 to print to screen, 2 to print to a file: ';
  9. chomp($outputOpt = <STDIN>); #chomp removes newline from input string
  10. print 'Search for: ';
  11. chomp(my $searchPattern = <STDIN>);
  12.  
  13.  
  14.  
  15.  
  16.  
  17. tie @lines, 'Tie::File', $inputFile, mode => O_RDONLY or die "Can't open $inputFile for reading: $!\n";
  18.  
  19. if (@results = grep(/$searchPattern/,@lines)){
  20.  
  21. if ($outputOpt == 1){
  22. print join("This box will close in 10 seconds\n",@results);
  23. }
  24. elsif ($outputOpt == 2){
  25. open(OUTPUT, ">$outputFile") or die "Can't open $outputFile for writing: $!\n";
  26. print OUTPUT join("\n",@results);
  27. close(OUTPUT);
  28. print "Everything is in a file called $outputFile\n";
  29. }
  30.  
  31. }
  32. else {
  33. print "No match & this box will close in 10 seconds\n";
  34. }
  35.  
  36. $num = 10;
  37. while($num--){
  38. sleep(1);
  39. }
Last edited by cscgal : Sep 20th, 2008 at 12:22 am. Reason: Added code tags
AddThis Social Bookmark Button
Reply With Quote  
Join Date: May 2006
Location: ★ ijug.net ★
Posts: 1,012
Reputation: ithelp will become famous soon enough ithelp will become famous soon enough 
Rep Power: 6
Solved Threads: 68
ithelp ithelp is offline Offline
Veteran Poster

Re: where would i find this information?

  #2  
Sep 19th, 2008
Use ~=m/ ... , do not use grep.
Reply With Quote  
Join Date: Sep 2008
Posts: 3
Reputation: 115cruiser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
115cruiser 115cruiser is offline Offline
Newbie Poster

Re: where would i find this information?

  #3  
Sep 19th, 2008
Originally Posted by ithelp View Post
Use ~=m/ ... , do not use grep.




so i should remove this line: if (@results = grep(/$searchPattern/,@lines)){
replace with ~=m/ ... ,


( i am reading up on ~=m/ ... , but am only finding info for m/ --- any suggestions
appreciated)
Reply With Quote  
Join Date: Mar 2006
Posts: 641
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 36
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Master Poster

Re: where would i find this information?

  #4  
Sep 19th, 2008
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.
Reply With Quote  
Join Date: Sep 2008
Posts: 3
Reputation: 115cruiser is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
115cruiser 115cruiser is offline Offline
Newbie Poster

Re: where would i find this information?

  #5  
Sep 19th, 2008
that appears to work as i don't receive an error when seperated by a space. but I am not
pulling any data that way ---

when i do single searches the data pulls up fine. Any suggestions helpful ---


I have learned lots of new things about grep today
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 6:31 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC