Solved

foreach loops: fence post problem

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
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 Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.

910 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

24 Experts available now in Live!

Get 1:1 Help Now