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 name; 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"}; } }