my $first = 0; # a flag to remember the first element while(elements to check) { # * if (elem should be printed) { # * if ($first == 0) { $first = 1; } # don't print a comma before the first element else { print ","; } # print a comma print elem # * } }