in reply to Loop Question

There isn't enough context here to give definitive answers to your questions.

As written, it's potentially an infinite loop. The loop will run while the condition $CustomerNumber = $Data{"CustomerNumber"} evaluates as true, which will be the case as long as the thing on the right-hand side of that equals sign evaluates as true (see What is true and false in Perl? for an explanation). Presumably, that should be == instead of = in that line (== is for testing, = is for assignment).

update I thought I'd put this in already, but I guess I hadn't : since it *appears* that the loop should only be executed once (you're not changing anything obviously related to the condition being tested), this should probably be an if rather than a while.

Within the loop, it looks like the code queries a database of some sort, and sets a few variables based on what it finds in the database. For any function you're having trouble understanding, read the output of

perldoc -f [function I don't know]
for more information.

HTH!

Philosophy can be made out of anything. Or less -- Jerry A. Fodor

Replies are listed 'Best First'.
Re: Re: Loop Question
by Anonymous Monk on Mar 21, 2001 at 21:39 UTC
    Actually this loop should execute until it finds another customer number. So by changing the equals sign to == instead of = will prevent the endless loop?

      That will solve the infinite loop problem, but it won't do what you want it to. The basic problem with this code is that neither $CustomerNumber nor $Data{CustomerNumber} are updated within the loop, so *something* will have to change (as written, it's sort of like asking "so, is this number odd?" about the same number over and over instead of (e.g.) "is 5 odd?" the first time, and then "is 6 odd?" the second time)

      You're using SQL, which standardly returns sets of results. The standard Perl DBI interface -- which you are not using here -- would have you do something like this:

      my $sth = $db->prepare("SELECT foo, bar FROM Cust_ac_da WHERE Customer +ID = ? AND #TranType= 'P'") or die "prepare failed: " . $db->errstr()."\n"; $sth->execute($CustomerNumber); while (my @stuff = $sth->fetchrow() ) { # do things with @stuff } $sth->finish;

      here, what happens is that the SQL statment is prepared, then executed (filling in the customer number where the ? occurs in the prepare); when the statement is executed, what happens is a result set is returned, and in this case each call to $sth->fetchrow() returns the next row in that set until there's nothing left in the set. Since you want to go on as long as you're still getting rows data from the database, you should have the test condition be the data-fetching statement.

      Things are complicated by the fact that you appear to be using a non-standard interface to your database (at a guess, the person who wrote it designed their own interface on top of DBI). This makes it much harder to recommend how exactly you should go on!

      A word of advice : if you're really new to programming and how control structures work, you should really bone up on the basics before you continue. DBI is getting pretty advanced for someone at your stage.

      Philosophy can be made out of anything. Or less -- Jerry A. Fodor

        Okay I understand what you are saying. But, I just want all information for the one customer number where the transtype=P. No other customer information should be shown. $CustomerNumber is a fixed variable that contains the customer number from the database that corresponds to the user at the website. How would I get the next row of data and get the $Data{"CustomerNumber"} from the database to test against the one on the website ($CustomerNumber) to execute the loop again for more information?
        Should this work?
        $SqlStatement=$db->prepare("SELECT TransDate, TransPmtAmt FROM Cust_ac +t_da WHERE CustomerNumber = ? AND TransType = 'P'") or die "prepare f +ailed: " . $db->errstr()."\n"; $SqlStatement->execute($CustomerNumber); while ( @stuff = $SqlStatement->fetchrow() ) { # do things with @stuff $FormatTransDate = substr($TransDate, 0, 10); $FormatTransPmtAmt = sprintf("\$%.2f", $TransPmtAmt); print $FormatTransDate; print "    "; print $FormatTransPmtAmt; } $SqlStatement->finish;