Ben Morrow <> wrote:
> Quoth :
> > ShaunJ <> wrote:
> > > I want to print a separator between each iteration of a foreach loop,
> > > but not after the last element. How do I avoid printing the last
> > > extraneous comma in the following code snippet?
> > >
> > > foreach (@ARGV) {
> > > print $_, ',';
> > > }
> > > print "\n";
> > >
> > > In this simple example join ',' would suffice, but the real body of
> > > the foreach loop is more complicated.
> >
> > Unless it is huge, you could just change the print to be push @results,
> > $_; and then do the join on @results.
>
> print join ',', map {...} @ARGV; is cleaner than pushing onto an
> intermediate array.
Sure, but I thought he was hinting that the contents of the loop were more
complex than shown, and hence would be more complex than comfortable in a
map block. Of course, different people do have different comfort levels
when it comes to cramming complexity into map blocks.
> Or you can set $, and $\ and avoid the join and "\n"
> altogether.
Sure, but then I'd have look up which ones are which (I don't use them
enough to remember $, from $" from $;, and $\ from $/) and then I'd worry
about whether I should change them globally (i.e. will code later on use
them implicitly and get confused, either now or with likely future
evolution of the script) or if I need to localize them and then go look up
the details of localizing punctuation variables. Easier to just use join,
which I don't need to look up. So basically, I don't use them because I
don't use them enough to feel comfortable using them without looking things
up, kind of a self-reinforcing situation.
A Freudian analysis of Perl programming technique.
Xho
--
--------------------
http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
this fact.