Solved

I need a faster solution for searching

Posted on 2002-05-08
5
202 Views
Last Modified: 2010-03-05
Hi ,

I'm currently doing a search using the method below and the spelling.txt file contains 20,000 lines. And my string $FullName is like "Mike_Abbas". When I run the search it's taking a very long time for each names and I have approximately 20,000 names. Any other better ways??

open F,'<C:\Nick\SingTelVoicePortal\Perl\spelling.txt';
@lines = <F>;
$num_match = grep /^$FullName$/i, @lines;
print $num_match;
0
Comment
Question by:n_chai
5 Comments
 
LVL 1

Accepted Solution

by:
japhyRPI earned 100 total points
ID: 6998066
Don't read the entire file at once, and don't use a regex.

  my $matches = 0;
  open FILE, "< $path" or die "can't read $path: $!";
  while (<FILE>) {
    chomp;
    $matches++ if $_ eq $FullName;
  }
  close FILE;
0
 
LVL 5

Expert Comment

by:amandeep
ID: 6999816
n_chai,

Try this:

my $num_match= 0;
my $FullName = "Mike_Abbas";
open(F,'<C:\Nick\SingTelVoicePortal\Perl\spelling.txt') or die "can't open file: $!";
while (<F>) {
   chomp;
   $num_match++ if(uc($_) eq uc($FullName));
}
close(F);
print $num_match;

0
 
LVL 8

Expert Comment

by:jhurst
ID: 7000314
Assuming use of a decent OS, such as unix:

$num_match=`grep -i \"$FullName\" yourFileName |wc -l`;

Should not be true but grep and wc are very efficient and the pipe lets them run in parallel.
0
 
LVL 84

Expert Comment

by:ozo
ID: 7000355
while( <FILE> ){
    chomp;
    $matches{uc $_}++;
}
foreach $FullName ( 'Mike_Abbas', 'n_chai' ...  ){
    print "$FullName: $matches{uc $FullName}\n";

}
0
 
LVL 6

Expert Comment

by:andreif
ID: 7000639
n_chai, you have a number of open questions where the answer is given. Please, take a look at those topics. If you didn't get answer, you can alwais ask more questions in that topics, but it's not very good idea to ignore given comments
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I've just discovered very important differences between Windows an Unix formats in Perl,at least 5.xx.. MOST IMPORTANT: Use Unix file format while saving Your script. otherwise it will have ^M s or smth likely weird in the EOL, Then DO NOT use m…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question