in reply to Detecting Last line of a file

The join method is definitely the way to go in this case, but to answer more generally (there was a recent node about this: Don't print on the last loop , but i can't find it at the moment Update Thanks omega_monk!), if you just flip the thought process and prepend commas instead of append them, it becomes:
my $addrCt = 0; foreach $address (@content) { print ", " if $addrCt++; # won't be done the first time chomp($address); print $address; }

Replies are listed 'Best First'.
Re^2: Detecting Last line of a file
by omega_monk (Scribe) on Jun 18, 2005 at 15:32 UTC
    I believe the node that you are referring to is 466164.
Re^2: Detecting Last line of a file
by GrandFather (Saint) on Jun 18, 2005 at 22:24 UTC

    Rather than keeping an extra variable around just test $address:

    print ", " if $address; # won't be done the first time

    Perl is Huffman encoded by design.
      Why wouldn't it be done the first time? That doesn't work unless $content[0] is somehow false .. since we're looping through @content and they're presumably all non-false, then $address is always set. Best way to get rid of the temp var is to just use split, or dpending on context (i.e. if you're reading a file) you could do print "," if $. > 1;.

        Quite right. That's what comes of answering before coffee! I was thinking in terms of concatenating the addresses into a variable which would be undef the first time round.


        Perl is Huffman encoded by design.