+ Reply to Thread
Results 1 to 4 of 4
  1. #1
    Member emcron's Avatar
    Join Date
    Mar 1999
    Location
    Universe
    Posts
    450

    Perl loop problem

    Does having an if statement inside a for loop cause problems? My problem is that I have an if statment inside a for loop and for some reason causes my for loop not to increment the value of i.

    ie.
    for($i=1; $i <= $num_checks; $i++)

    Below is the sub- routine thats causing me problems. I know there are a lot of things I can to to make this shorter, but I'm fairly new to perl and need to get this done quick.

    sub error_checking {
    for($i=1; $i <= $num_checks; $i++)
    {
    print "************************************************* **************\n";
    print "* Worcester Youth Center Check # $i\n";
    print "* 27 Chandler St.\n";
    print "* Worcester, Ma 01609\n";
    print "* Date $date[]\n";
    print "*\n";
    print "* Pay to: $pay_to[i] \$$amount_num[i]\n";
    print "*\n";
    print "* Note: $note[i]\n";
    print "************************************************* **************\n";
    if ($i%2 == 0)
    {
    print "Are these checks correct? (y/n) ";
    chomp ($yesno = <STDIN>);
    if ($yesno eq "n") {
    &edit;}
    }


    }
    if ($i%2){
    print "Are these checks correct? (y/n) ";
    chomp ($yesno = <STDIN>);
    if ($yesno eq "n") {
    &edit;}}
    }
    The best defense against terrorism is not war, but justice.

  2. #2
    Member
    Join Date
    Jun 1999
    Posts
    307
    Not a Perl programmer. Where is num_checks initialized? Is it a global that has a value >= 1? If it is not and it has a value of 0, you will never get into the loop.

    Since i == 1 you will never loop: i <= num_checks :: 1 <= 0.

  3. #3
    Member emcron's Avatar
    Join Date
    Mar 1999
    Location
    Universe
    Posts
    450
    $num_checks will always be >=1, if it wasn't the program wouldn't reach that subroutine.
    The best defense against terrorism is not war, but justice.

  4. #4
    Banned qball's Avatar
    Join Date
    Oct 1999
    Location
    Rockaway, NJ 07866
    Posts
    1,730
    a conditional (if) inside a loop (for) will only cause problems (fingers crossed as people do stupid stuff) if you mess with the incrementing loop var, in this case $i.

    Now, you a using $i for each loop.

    replace:

    "for($i=1; $i <= $num_checks; $i++) "

    wif:

    "for($uniquename=1; $uniquename <= $num_checks; $uniquename++)"


    You may also have an issue with $num_checks.

    Keep your variables in scope, I think you can figure out which loop is the problem.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts







New Security Features Planned for Firefox 4
Another Laptop Theft Exposes 21K Patients' Data
Oracle Hits to Road to Pitch Data Center Plans
Microsoft Preps Array of Windows Patches
Microsoft Nears IE9 Beta With Final Preview
Simplified Analytics Improve CRM, BI Tools
Android Passes RIM as Top Mobile OS in 2Q
VMware Updates Hyperic System Management
File Monitoring Key to Enterprise Security
LinkedIn Snaps Up SaaS Player mSpoke