Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Print a separator between each iteration of a foreach loop

Reply
Thread Tools

Print a separator between each iteration of a foreach loop

 
 
ShaunJ
Guest
Posts: n/a
 
      04-15-2008
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. What I want to do is something
like this:

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

Thanks,
Shaun
 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      04-15-2008
ShaunJ <(E-Mail Removed)> 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 (@ARGV) {
> print $_;
> print ',' unless last_element;
>}
>print "\n";


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

OR

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

jue
 
Reply With Quote
 
 
 
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-15-2008
ShaunJ <(E-Mail Removed)> 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.


> What I want to do is something
> like this:
>
> foreach (@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<@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.
 
Reply With Quote
 
Willem
Guest
Posts: n/a
 
      04-15-2008
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. What I want to do is something
) like this:
)
) foreach (@ARGV) {
) print $_;
) print ',' unless last_element;
) }
) print "\n";

One sometimes-seen useful trick is this:

my $sep = '';
foreach (@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
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 1:24 pm, ShaunJ <(E-Mail Removed)> 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. What I want to do is something
> like this:
>
> foreach (@ARGV) {
> print $_;
> print ',' unless last_element;}
>
> print "\n";
>
> Thanks,
> Shaun



my $notlast = $#ARGV;
foreach (@ARGV) {
print $_;
last unless $notlast;
$notlast--;
print ',';
}
 
Reply With Quote
 
ShaunJ
Guest
Posts: n/a
 
      04-15-2008
On Apr 15, 10:36 am, Frank Seitz <(E-Mail Removed)> wrote:
> ShaunJ wrote:
> > foreach (@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 (@_) {
print $_;
} separator {
print ',';
}

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

Cheers,
Shaun
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-15-2008
>>>>> "W" == Willem <(E-Mail Removed)> writes:
W> One sometimes-seen useful trick is this:

W> my $sep = '';
W> foreach (@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 ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      04-15-2008
>>>>> "S" == ShaunJ <(E-Mail Removed)> writes:

S> On Apr 15, 10:36 am, Frank Seitz <(E-Mail Removed)> wrote:
>> ShaunJ wrote:
>> > foreach (@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 (@_) {
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 ------ (E-Mail Removed) -------- 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 ---------
 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      04-15-2008

Quoth (E-Mail Removed):
> ShaunJ <(E-Mail Removed)> 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. Or you can set $, and $\ and avoid the join and "\n"
altogether.

Ben

 
Reply With Quote
 
xhoster@gmail.com
Guest
Posts: n/a
 
      04-15-2008
Ben Morrow <(E-Mail Removed)> wrote:
> Quoth (E-Mail Removed):
> > ShaunJ <(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
How to queue each loop iteration into a thread pool, with a max size? David Karr Perl Misc 2 04-07-2011 03:00 PM
Try to exit inner loop and start next iteration of outter loop. Nene Perl Misc 6 12-13-2008 12:20 AM
Struts - Problem with nested iteration or double iteration Rudi Java 5 10-01-2008 03:30 AM
open new file each loop iteration Danny Anderson C++ 0 01-21-2004 08:56 PM



Advertisments