Solved

foreach loops: fence post problem

Posted on 2004-08-19
5
509 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
5 Comments
 
LVL 18

Assisted Solution

by:kandura
kandura earned 100 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 75 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 75 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now