Solved

foreach loops: fence post problem

Posted on 2004-08-19
5
519 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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
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…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

786 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