naveed010 has asked for the wisdom of the Perl Monks concerning the following question:

OK, I've been beating my head against the wall for most of the morning on this... I have to ask for help.

use Config::Simple; $cfg = new Config::Simple(); $cfg->read('c:\work\ftp_config_file.txt'); for ($ct=0, $cfg->param("FTP".$ct.".targetIP")!= '', $ct++) { print " FTP$ct value: ".$cfg->param("FTP$ct.targetIP")."\n"; }

Is displaying the following output:


FTP1 value: 1.1.1.1
FTP1 value: 1.1.1.1
FTP1 value: 1.1.1.1

The c:\work\ftp_config_file.txt contains the following values:

[FTP1] targetIP=1.1.1.1 [FTP2] targetIP=10.10.10.10

For the life of me, I can't see what is causing this to loop three times without incrementing the counter..

I get the correct output when I run the script as such:

use Config::Simple; $cfg = new Config::Simple(); $cfg->read('c:\work\ftp_config_file.txt'); my $ct=1; print " FTP$ct value: ".$cfg->param("FTP$ct.targetIP")."\n"; $ct++; print " FTP$ct value: ".$cfg->param("FTP$ct.targetIP")."\n";

thanks in advance those monks who are most intelligent at deciphering a moron's code. :)

Replies are listed 'Best First'.
Re: Need Debug Help
by kyle (Abbot) on Jan 18, 2008 at 18:01 UTC

    The kind of for loop you're trying to write takes its terms separated by semicolons:

    for ( my $i = init(); $i != condition(); $i++ ) {}

    What you actually wrote here has commas instead, so it's taking that as a list of three things to loop over.

    for ( 1, 2, 3 ) {}

    So change those commas to semicolons, and that should help.

      Kyle, you da man.

      Thanks much. Works like a charm.

      for ($ct=1; $cfg->param("FTP".$ct.".targetIP")!= ''; $ct++)
      I'm using a blend of perl and Oracle and the Oracle's for( x, x<y, x++) statement uses comments.

      DOH! :)

        Careful man,

        Oracle's for loops' syntax is
        FOR counter IN [REVERSE] lowest..highest LOOP {.statements.} END LOOP;
Re: Need Debug Help
by apl (Monsignor) on Jan 18, 2008 at 18:02 UTC
    You want to use semicolons, not commas, to sepearate the three clauses of your for statement. That is:

    for ($ct=0; $cfg->param("FTP".$ct.".targetIP")!= ''; $ct++)
Re: Need Debug Help
by toolic (Bishop) on Jan 18, 2008 at 18:01 UTC
    In my opinion, that is an odd-looking for statement. Typically, the end condition would be a constant value. I think the more conventional approach would be to use a while loop to check for a value != ''.

    I realize that this does not explain the behavior that you are seeing, but perhaps this would be a cleaner solution.