where would i find this information?

Please support our Perl advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Sep 2008
Posts: 3
Reputation: 115cruiser is an unknown quantity at this point 
Solved Threads: 0
115cruiser 115cruiser is offline Offline
Newbie Poster

where would i find this information?

 
0
  #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
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 1,857
Reputation: ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all ithelp is a name known to all 
Solved Threads: 120
ithelp's Avatar
ithelp ithelp is offline Offline
Posting Virtuoso

Re: where would i find this information?

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

Re: where would i find this information?

 
0
  #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 Quick reply to this message  
Join Date: Mar 2006
Posts: 898
Reputation: KevinADC has a spectacular aura about KevinADC has a spectacular aura about 
Solved Threads: 67
KevinADC's Avatar
KevinADC KevinADC is offline Offline
Practically a Posting Shark

Re: where would i find this information?

 
0
  #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 Quick reply to this message  
Join Date: Sep 2008
Posts: 3
Reputation: 115cruiser is an unknown quantity at this point 
Solved Threads: 0
115cruiser 115cruiser is offline Offline
Newbie Poster

Re: where would i find this information?

 
0
  #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 Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for Perl
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC