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

Re: [MacPerl] Some suggestions?



Allan Greenier wrote:
> 
> Greetings fellow users of the sublime MacPerl,
> 
> I'm making a CGI search engine. Any suggestions as far as fastest way to
> search and optimization are appreciated.
> 
> The engine will search a list that looks like this:
> ("Data<TAB>Data<TAB>Data<TAB>Data","Data<TAB>Data<TAB>Data<TAB>Data").
> This list will be 500 items long. Sometimes it needs to search in the
> first item, sometimes the others. Usually only one item.
> So i do:
> 
> foreach $item (@thelist){
> @itemarray = split (/\t/,$item)
> if $itemarray[0] eq $searchitem{
> dosomething }
> }
> 
> I'm concerned that I could search 2000 (4x500) items before I get to one
> I want. After I find that one, I will need to extract $itemarray[3]
> everytime.
> 
> So how do I construct this in the quickest way possible??

Sounds like you want a hash. This will likely result in a much quicker
find that the array scanning. However, you will have to reorganize your
data a little.

It looks like you have $key1<tab>$key2<tab>$key3<tab>$data, from what
you have said. Your snippet only looks at key1, but I think you stated
you want it to look at key2 ($itemarray[1]) and key3 ($itemarray[2]) as
well. You always want to return $item[3], which I have called $data.

You could construct a hash:
$key1 => $data1
$key2 => $data1
$key3 => $data1
$key4 => $data2
$key5 => $data2
$key6 => $data2
...

The just search the keys and return the data. Each data item has three
keys pointing to it. 

#!/usr/bin/perl -w

%mydata = 
  ( collie => 'dog',
    shepherd => 'dog',
    poodle => 'dog',
    siamese => 'cat',
    calico=> 'cat',
    tabby => 'cat',
  );

print "Name that animal: ";
chomp($animal = <STDIN>);

print "$mydata{$animal}\n";
 
__END__


Name that animal: collie
dog

Hope that helps,
Geoff

===== Want to unsubscribe from this list?
===== Send mail with body "unsubscribe" to macperl-request@macperl.org