Print a separator between each iteration of a foreach loop

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 ( [at] ARGV) {
print $_, ',';
}
print "\n";

In this simple example join ',' would suffice, but the real body of
the foreach loop is more complicated. What I want to do is something
like this:

foreach ( [at] ARGV) {
print $_;
print ',' unless last_element;
}
print "\n";

Thanks,
Shaun
Shaun Jackman [ Di, 15 April 2008 19:24 ] [ ID #1943150 ]

Re: Print a separator between each iteration of a foreach loop

ShaunJ wrote:

> foreach ( [at] ARGV) {
> print $_;
> print ',' unless last_element;
^^^^^^^^^^^^
\$_ eq \$ARGV[-1]
> }
> print "\n";

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
Frank Seitz [ Di, 15 April 2008 19:36 ] [ ID #1943152 ]

Re: Print a separator between each iteration of a foreach loop

ShaunJ <sjackman [at] gmail.com> wrote:
>I want to print a separator between each iteration of a foreach loop,
>but not after the last element.

That's a standard problem in CS. You need to process the first or last
element outside the loop.

> How do I avoid printing the last
>extraneous comma in the following code snippet?
>[...] What I want to do is something like this:
>
>foreach ( [at] ARGV) {
> print $_;
> print ',' unless last_element;
>}
>print "\n";

print $ARGV[0]; shift [at] ARGV
for ( [at] ARGV) {
print ',';
print $_;
}

OR

for ( [at] ARGV[0.. [at] ARGV-2]) {
print $_;
print ',';
}
print $ARGV[$#ARGV];

jue
jurgenex [ Di, 15 April 2008 19:40 ] [ ID #1943154 ]

Re: Print a separator between each iteration of a foreach loop

ShaunJ <sjackman [at] gmail.com> 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 ( [at] 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 [at] results, $_;
and then do the join on [at] results.


> What I want to do is something
> like this:
>
> foreach ( [at] ARGV) {
> print $_;
> print ',' unless last_element;
> }
> print "\n";

I don't think there is a clean way to do it with a foreach loop. You
could use a counter, but if you are going to do that I'd just as soon
change to a C style loop:

for (my $i=0; $i<=$#ARGV; $i++) {
print $ARGV[$i];
print "," unless $i==$#ARGV;
};

But actually, I might go for:

for (my $i=0; $i< [at] ARGV; $i++) {
print "," unless $i==0;
print $ARGV[$i];
};

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.
xhoster [ Di, 15 April 2008 19:44 ] [ ID #1943155 ]

Re: Print a separator between each iteration of a foreach loop

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 ( [at] ARGV) {
) print $_, ',';
) }
) print "\n";
)
) In this simple example join ',' would suffice, but the real body of
) the foreach loop is more complicated. What I want to do is something
) like this:
)
) foreach ( [at] ARGV) {
) print $_;
) print ',' unless last_element;
) }
) print "\n";

One sometimes-seen useful trick is this:

my $sep = '';
foreach ( [at] ARGV) {
print $sep; $sep = ',';
print $_;
}


SaSW, Willem
--
Disclaimer: I am in no way responsible for any of the statements
made in the above text. For all I know I might be
drugged or something..
No I'm not paranoid. You all think I'm paranoid, don't you !
#EOT
Willem [ Di, 15 April 2008 19:47 ] [ ID #1943156 ]

Re: Print a separator between each iteration of a foreach loop

On Apr 15, 1:24 pm, ShaunJ <sjack... [at] gmail.com> 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 ( [at] ARGV) {
> print $_, ',';}
>
> print "\n";
>
> In this simple example join ',' would suffice, but the real body of
> the foreach loop is more complicated. What I want to do is something
> like this:
>
> foreach ( [at] ARGV) {
> print $_;
> print ',' unless last_element;}
>
> print "\n";
>
> Thanks,
> Shaun


my $notlast = $#ARGV;
foreach ( [at] ARGV) {
print $_;
last unless $notlast;
$notlast--;
print ',';
}
smallpond [ Di, 15 April 2008 19:48 ] [ ID #1943157 ]

Re: Print a separator between each iteration of a foreach loop

On Apr 15, 10:36 am, Frank Seitz <devnull4... [at] web.de> wrote:
> ShaunJ wrote:
> > foreach ( [at] ARGV) {
> > print $_;
> > print ',' unless last_element;
>
> ^^^^^^^^^^^^
> \$_ eq \$ARGV[-1]
>
> > }
> > print "\n";

That works. Thanks, Frank!

This problem is common enough that it almost warrants special syntax,
similar to the contine block.

foreach ( [at] _) {
print $_;
} separator {
print ',';
}

Not a serious proposal, just a "wouldn't it be nice if".

Cheers,
Shaun
Shaun Jackman [ Di, 15 April 2008 20:00 ] [ ID #1943159 ]

Re: Print a separator between each iteration of a foreach loop

>>>>> "W" == Willem <willem [at] stack.nl> writes:
W> One sometimes-seen useful trick is this:

W> my $sep = '';
W> foreach ( [at] ARGV) {
W> print $sep; $sep = ',';

minor optimization which works but is data dependent:

print $sep;
$sep ||= ',';

W> print $_;

and $_ is the default for print (as it is for many other funcs ).

and if it is really just the argv array, join has to be better. the
array would have to be slightly oversized to make join slow down.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Uri Guttman [ Di, 15 April 2008 20:11 ] [ ID #1943160 ]

Re: Print a separator between each iteration of a foreach loop

>>>>> "S" == ShaunJ <sjackman [at] gmail.com> writes:

S> On Apr 15, 10:36 am, Frank Seitz <devnull4... [at] web.de> wrote:
>> ShaunJ wrote:
>> > foreach ( [at] ARGV) {
>> > print $_;
>> > print ',' unless last_element;
>>
>> ^^^^^^^^^^^^
>> \$_ eq \$ARGV[-1]
>>
>> > }
>> > print "\n";

S> That works. Thanks, Frank!

S> This problem is common enough that it almost warrants special syntax,
S> similar to the contine block.

S> foreach ( [at] _) {
S> print $_;
S> } separator {
S> print ',';
S> }

S> Not a serious proposal, just a "wouldn't it be nice if".

perl6 has those flow control blocks. there is FIRST, LAST, BEGIN and a
bunch of others (all upper case names) that trap conditions/exceptions
inside their outer blocks.

uri

--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Free Perl Training --- http://perlhunter.com/college.html ---------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
Uri Guttman [ Di, 15 April 2008 20:13 ] [ ID #1943162 ]

Re: Print a separator between each iteration of a foreach loop

Quoth xhoster [at] gmail.com:
> ShaunJ <sjackman [at] gmail.com> 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 ( [at] 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 [at] results, $_;
> and then do the join on [at] results.

print join ',', map {...} [at] ARGV; is cleaner than pushing onto an
intermediate array. Or you can set $, and $\ and avoid the join and "\n"
altogether.

Ben
Ben Morrow [ Di, 15 April 2008 20:31 ] [ ID #1943166 ]

Re: Print a separator between each iteration of a foreach loop

Ben Morrow <ben [at] morrow.me.uk> wrote:
> Quoth xhoster [at] gmail.com:
> > ShaunJ <sjackman [at] gmail.com> 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 ( [at] 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 [at] results,
> > $_; and then do the join on [at] results.
>
> print join ',', map {...} [at] 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.
xhoster [ Di, 15 April 2008 21:40 ] [ ID #1943171 ]

Re: Print a separator between each iteration of a foreach loop

Hi everyone,

> foreach ( [at] ARGV) {
> print $_;
> print ',' unless last_element;}
>
> print "\n";
Could this version be useful to you? :

---/cut/---
#!/usr/bin/perl

my [at] a = qw(1 2 3 4 5);

for my $i (0..$#a) {
print $a[$i] . (($i < $#a) ? ", " : "\n");
}

1;
---/cut/---

Bye.
ff0000
ff0000 [ Mi, 16 April 2008 00:15 ] [ ID #1943185 ]

Re: Print a separator between each iteration of a foreach loop

On Tue, 15 Apr 2008 10:48:55 -0700 (PDT), smallpond <smallpond [at] juno.com> wrote:
>
> my $notlast = $#ARGV;
> foreach ( [at] ARGV) {
> print $_;
> last unless $notlast;
> $notlast--;
> print ',';
> }

Which can be simplified a little to:

my $notlast = $#ARGV;
foreach ( [at] ARGV) {
print $_;
print ',' if $notlast--;
}
Dave Weaver [ Mi, 16 April 2008 12:55 ] [ ID #1944072 ]

Re: Print a separator between each iteration of a foreach loop

On 2008-04-15 19:40, xhoster [at] gmail.com <xhoster [at] gmail.com> wrote:
> Ben Morrow <ben [at] morrow.me.uk> wrote:
>> Quoth xhoster [at] gmail.com:
>> > ShaunJ <sjackman [at] gmail.com> 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 ( [at] 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 [at] results,
>> > $_; and then do the join on [at] results.
>>
>> print join ',', map {...} [at] 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.

One could argue that if it is complex it should be put into a sub.
So you could write

print join ',', map process_one_arg($_), [at] ARGV

where process_one_arg is a suitable descriptive sub name, of course.

hp
hjp-usenet2 [ Do, 17 April 2008 14:16 ] [ ID #1944844 ]
Perl » comp.lang.perl.misc » Print a separator between each iteration of a foreach loop

Vorheriges Thema: Perl's @INC - am I missing the point?
Nächstes Thema: Some questions about q{} and qr{}.