Perl Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPerl Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old September 24th, 2000, 06:12 PM
scream scream is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 441 scream User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 22 h 59 m 38 sec
Reputation Power: 9
Send a message via ICQ to scream
Can someone tell me what is wrong with my for looping? This script goes through a mysql database and checks the validity of each url. After all the urls have been checked it just keeps looping and DBI generates an error that says "fetchrow_array failed: fetch() without execute()" over and over. I think the problem has to do with my misunderstanding of the line $numrows = $sth->rows. Please help me, here is my code:

<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
#!/usr/bin/perl

use LWP::UserAgent;
use DBI;

$db_database = "db";
$db_uid = "root";
$db_pwd = "password";
($ua = LWP::UserAgent->new)->timeout(20); #actually set timeout


$dbh = DBI->connect ("DBI:mysql:$db_database".$mysqlsock, $db_uid, $db_pwd) or die("could not connect to dbn");
$sth = $dbh->prepare("SELECT url FROM files");
$sth -> execute();
$numrows = $sth->rows;

print "nn";
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);

if(($ua->request(HTTP::Request->new('HEAD', $url)))->is_success()) {
$validity = "link works";
} else {
$validity = "link sucks";
$valid_update = $dbh->do("UPDATE files SET valid = valid + 1 WHERE url = '$url'");
}
print "$validityn$urlnn";
}
[/code]

Reply With Quote
  #2  
Old September 25th, 2000, 03:10 AM
CujoRbd CujoRbd is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2000
Posts: 16 CujoRbd User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0


well, i'm not exactly the best with Perl, but i noticed a couple things off the bat.

i think that this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>for ($i = 0; $i = $numrows; ++$i) {[/code]
should be this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>for ($i = 0; $i = $numrows; $i++) {[/code]

secondly, i think that you can't define the '$i' twice (or rather, it needs not be and therefore could be posing a problem), as it sort of poses a conflict of interest. so further, you may want to change it to this:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>for ($i; $i = $numrows; $i++) {[/code]

and as a suggestion (an unnecessary change, though), you might want to squeeze that together, as Perl will output it better. so further:
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>for ($i;$i=$numrows;$i++) {[/code]


i hope this could help!


Cujo




[This message has been edited by CujoRbd (edited September 25, 2000).]

Reply With Quote
  #3  
Old September 26th, 2000, 02:43 AM
billyo billyo is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 2000
Posts: 114 billyo User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 9
Keep in mind that a for loop can be equivacated to a while loop:

for ($i=0;$i<$numrows;$i++)
{
// code
}

is the same as:
$i=0;
while ($i<$numrows)
{
// code
$i++;
}

so you see, the for loop also has a conditional statement, in the code you posted it is $i=$numrows, which is where your problem is. "=" is an assignment operator, and because $i can be assigned to $numrows, the operation returns true, so your conditonal is always true. You need to use a comparison operator, such as "==" or "<=" which will at some point return false and cease the execution of your loop. Incedentally,
$i=0;
$a=++$i; // $a=1, $i=1
$a=$i++ // $a=0; $i=1

So i don't think this will have a huge effect on your loop.

Reply With Quote
  #4  
Old September 29th, 2000, 12:53 PM
donarb donarb is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jun 1999
Location: Seattle
Posts: 133 donarb User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 10
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by scream:
Can someone tell me what is wrong with my for looping?
<BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">code:</font><HR><pre>
for ($i = 0; $i = $numrows; ++$i) {
$url = ($sth->fetchrow_array);
...
}
[/code]
[/quote]


If you have no need for the count of rows that are returned you can just use this while loop, it will keep returning rows until there are no more left.

while (my $url = $sth->fetchrow) {
#process $url here
}

You don't need to use fetchrow_array if the row you are returning consists of just one field.

Don

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPerl Programming > for loop - MY LOGIC IS FLAWED


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 3 hosted by Hostway