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

Ah yes. Your monkish "Cruise Ship" director here. I thought this simple conditional would work just fine but I seem to have just missed some fine piece of logic.

I'm dumping an extracted HTML table of Cruise Ships (table is SHIPNAME,DATETIME,LATITUDE,LONGITUDE,CALLSIGN) to a csv file and one of the lines in the table is a break saying that the following "Ships in port or not reporting today:". So I thought I would break out of processing that line and then set a flag for all remaining ships ($in_port = "true").

However, all the output shows that the flag isn't being set and the line isn't being skipped in the output. Why is that? It's probably a real simple thing but I'm not seeing it.

my $in_port; foreach my $ts ($te->tables) { foreach my $row ($ts->rows) { if ($row != "Ships in port or not reporting today:") # not a ship n +ame; skip this and set the in_port flag for all remaining ships { $in_port = "false"; $out_fh->print (join(',', @$row), ",", $in_port, "\n"); } else {$in_port = "true"}; } }

Replies are listed 'Best First'.
Re: Setting flags in a conditional
by BrowserUk (Patriarch) on Mar 20, 2008 at 05:55 UTC
      Ooh! I thought it was simple.

      Except, it still isn't working. I also get a

      Use of uninitialized value $row in join or string at test3.pl line 80.
      error, which is the $out_fh -> print... line. Any other thoughts? Thanks for the quick look at this, much appreciated. Matt
        I thought maybe it was the location of the $in_port flag initialization - it should not be in the loop. But it's still not skipping the line nor is it setting the flag. I must have a test/condition problem. I'll look there. I'm pretty sure the code should look like this:

        my $in_port; $in_port = "false"; foreach my $ts ($te->tables) { foreach my $row ($ts->rows) { if ($row ne "Ships in port or not reporting today:") # not a ship n +ame; skip this and set the in_port flag for all remaining ships { $out_fh->print (join(',', @$row), ",", $in_port, "\n"); } else {$in_port = "true"}; } }