in reply to printing lists...

You don't know the last element ... but you know the first. Using that knowledge, you can enhance your logic as follows:

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 th +e first element else { print ","; } # print a comma print elem # * } }

HTH, Rata

Update: marked the pseudocode of the original poster with # * to reduce confusion

Replies are listed 'Best First'.
Re^2: printing lists...
by ww (Archbishop) on Jul 27, 2010 at 11:54 UTC
    Clever approach. Minor glitch in implementation.
    Update: that should be "glitch in my interpretation." Neither Ratazong's nor OP's data is necessarsarily formatted as I did it.

    my $first=0; while (<DATA>) { chomp ($_); my $element = $_; if ($first == 0) { $first = 1; # don't print a comma before the first element } else { print ","; # print a comma } print $element; } print "\n"; __DATA__ foo bar blivitz this is a line 1 2 a b cde

    Ratazzong produces:

    foo ,bar ,blivitz ,this is a line ,1 ,2 ,a ,b ,cde

    instead of (what I understand to be) OP's desire:

    foo,bar,blivitz,this is a line,1,2,a,b,cde