|
From: ShaunJ on 15 Apr 2008 13:24 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
From: Frank Seitz on 15 Apr 2008 13:36 ShaunJ wrote: > foreach (@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
From: J�rgen Exner on 15 Apr 2008 13:40 ShaunJ <sjackman(a)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 (@ARGV) { > print $_; > print ',' unless last_element; >} >print "\n"; print $ARGV[0]; shift @ARGV for (@ARGV) { print ','; print $_; } OR for (@ARGV[0..(a)ARGV-2]) { print $_; print ','; } print $ARGV[$#ARGV]; jue
From: xhoster on 15 Apr 2008 13:44 ShaunJ <sjackman(a)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 (@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.
From: Willem on 15 Apr 2008 13:47 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
|
Next
|
Last
Pages: 1 2 3 Prev: PERL to mean what 'perldoc perl' says is wrong? Next: FAQ 9.15 How do I parse a mail header? |