?
Solved

foreach loops: fence post problem

Posted on 2004-08-19
5
Medium Priority
?
546 Views
Last Modified: 2012-06-27
I'd like to print out the strings in an array seperated by commas. The following code would work, except that the final comma is not followed by an element.

foreach my $e (@elements)
{
    print $e . ", ";
}

How can I write code that does not print the final comma? Thanks.
0
Comment
Question by:bobwood2000
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 18

Assisted Solution

by:kandura
kandura earned 400 total points
ID: 11839541
Two suggestions:

1. check the entry for "join" in the perlfunc manpage.
2. check the meaning of $, in the perlvar manpage.
0
 
LVL 1

Assisted Solution

by:lth2h
lth2h earned 300 total points
ID: 11844621
When I have to do something like this.  I first append the info to a variable, then use a regex to remove the extra comma, then print the variable.
0
 
LVL 1

Expert Comment

by:lth2h
ID: 11844649
Oh I forgot to add..

You can also include a counting variable and do it like this...


my $i=0;
foreach my $e (@array) {

if ($i == length(@array)) {
print $e;

} else {

print $e . ", ";

}

$i++;

}
0
 
LVL 1

Accepted Solution

by:
JustinPincar earned 300 total points
ID: 11845158
Theres a really easy way to do this.

Set the perl special list-seperator variable $, to something and then print the array.
For example...

@examples = ("hello", "goodbye", "cheesecake");

$, = ", ";

print @examples;

That will give you: hello, goodbye, cheesecake

~Justin
0
 

Author Comment

by:bobwood2000
ID: 11845217
Thanks! Using either $, or join is very elegant.
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
I have been pestered over the years to produce and distribute regular data extracts, and often the request have explicitly requested the data be emailed as an Excel attachement; specifically Excel, as it appears: CSV files confuse (no Red or Green h…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Six Sigma Control Plans

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question