%seen = (); @uniq = grep { ! $seen{$_} ++ } @z; my $query = q|SELECT weburl,datecode,idnum FROM maindb WHERE idnum IN (| . join(',', map { $dbh->quote($_) } @uniq) . q|)|; my($sth) = $dbh->prepare($query); $sth->execute || die("Could not execute!"); my $array_ref = $sth->fetchall_arrayref(); foreach my $row (@$array_ref) { my ( $url, $dc, $idn ) = @$row; if ($url){ $ua = new LWP::UserAgent; $ua->agent ('Mozilla/4.0 (compatible; MSIE 5.03; Windows 95)'); my @imgs = (); sub callback { my($tag, %attr) = @_; return if $tag ne 'img'; push(@imgs, values %attr); } $p = HTML::LinkExtor->new(\&callback); $res = $ua->request(HTTP::Request->new(GET => $url), sub {$p->parse($_[0])}); my $base = $res->base; @imgs = map { $_ = url($_, $base)->abs; } @imgs; @foo = grep /\.jpg/i, @imgs; $randompic = $foo[rand @foo]; $req1 = new HTTP::Request 'GET' => $randompic; my $img_response = $ua->request($req1); $content = $img_response->content; open(TGPMAIN,">$cat_html/featured/$idn\.jpg") || print "Can't open gall REASON: ($!)\n"; print TGPMAIN "$content"; close(TGPMAIN); $sql = "INSERT INTO photofour VALUES('$idn','$dc','$randompic')"; $dbh->do($sql); } }