[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

[MacPerl] Re: Efficient Search in Perl?



At 12.59 1998.03.19, Shyam Hegde wrote:
>The first file is over 3meg and the second is over 9meg.  At the moment all
>I am doing is reading the file line-by-line in a while loop and using the
>Perl 'eq' to do a string match.  Can you think of a smarter way of doing
>this??
>I have thought of firstly sorting both files so that at least the two key
>fields take the same order, but I'm not sure if even this will help any.
>Algorithms and Data Structures never was my strong point!

If it was me, I would sort the files by numerical order of the first field,
and then use Search::Dict to do a binary search on the file.

#!perl -wl
use Search::Dict;
open(FH, ':file') || die $!;
$key = '        100002086';

look(*FH, $key);
$x = <FH>;
print $x;

This will print the first occurrence of the key, which is:

        100002086,'life_form'

Of course, take out the whitespace if it is not needed.  Search::Dict does
very fast searching, even over large files, but only if the lines are in
order (having the numbers in order is sufficient).

Hope this helps,

--
Chris Nandor          mailto:pudge@pobox.com         http://pudge.net/
%PGPKey=('B76E72AD',[1024,'0824 090B CE73 CA10  1FF7 7F13 8180 B6B6'])
#==               New Book:  MacPerl: Power and Ease               ==#
#==    Publishing Date: Early 1998. http://www.ptf.com/macperl/    ==#



***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch