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


In reply to Re: Re: Re: Loop Question by arturo
in thread Loop Question by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.