> This is my first ever Perl script (!)
>
 Good for you!
 
> The script is supposed to continously query google for hits 
> on the "search google for this string" and store the returnde 
> URLs to database. In the scipt below it should query 5 times 
> (0-50 hits, 10 hits returned per query) however if i set the 
> loop to run till $counter<20 it loops up to 30, if I loop 
> till $counter<50 it actually loops until 70, etc 
> As far as I can gather - Im "looping to much" and I think the 
> problem has to do with the search->response being either 
> array or ref array ... Im not at all familiar with these.
> 
> I suspect something needs to be reset to an empty array - I 
> have a feeling its accumulating all previous results along 
> with each new loop ...

No it probably has to do with only being incremented in certain 
circumstances in which case it will be less than 50 for longer.

To illustrate:
my ($c,$n);
while($c < 50) {
        if($c =~ m/0$|2$|4$|6$|8$/){ $c = $c - 3; } 
        else { $c = $c+2; }
        $n++;
}
$n will be 28 because it took 28 loops for $c to be greater than 50
But if you change it to:
while($c < 50) {
        if($c =~ m/0$|2$|4$|6$|8$/){ $c++; }
} 

it will be an infiniite loop because $c will always be less than 50 

HTH

DMuey


> 
> 
> use Net::Google;
> use DBI;
> use constant LOCAL_GOOGLE_KEY => "My Google Key";
> 
> my $google = Net::Google->new(key=>LOCAL_GOOGLE_KEY);
> my $search = $google->search();
> my $counter = 0;
> my $dbh = DBI->connect ('dbi:mysql:choogle', 'soren', 'letmein') 
>         || die "Couldn't open DB connection.";
> 
> while ($counter<50) { 
>         $search->query("search google for this string");
>         $search->lr(qw(da)); # danish
>         $search->ie("utf8");
>         $search->oe("utf8");
>         $search->starts_at($counter);
>         $search->max_results($counter+10);
>         $search->restrict(qw(countryDK));
>                 
>         foreach my $r (@{$search->response()}) { # Loop 
> search response
> 
>                 print "Now collecting results ".$counter." to 
> ".$counter+10;
>                 foreach my $element (@{$r->resultElements()}) 
> { # Loop resultElements
>                         $dbh->do ("INSERT IGNORE INTO 
> temp_hits VALUES (".
>                                                               
>          
> '"'.$element->URL().'", '.
>                                                               
>          
> '"'.$element->title().'", '.
>                                                               
>          
> 'NOW(), '.
>                                                               
>          
> '"'.$search->query().'")')
>                         || die ("Couldn't write to DB");
>                 }  
>         $counter = $counter + 10;
>         }
> }
> 
> 
> 
> 
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to