Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > How can I remove trailing commas?

Reply
Thread Tools

How can I remove trailing commas?

 
 
Jame Pearl
Guest
Posts: n/a
 
      01-30-2006
How can I remove any trailing commas from the end of $var?

I tried using

$var =~ s/,+$//

but it didn't work.

 
Reply With Quote
 
 
 
 
Paul Lalli
Guest
Posts: n/a
 
      01-30-2006
Jame Pearl wrote:
> How can I remove any trailing commas from the end of $var?
>
> I tried using
>
> $var =~ s/,+$//
>
> but it didn't work.


"Didn't work" is the worst of all possible error descriptions. *How*
did it not work? Did it not remove anything? Did it remove too much?
Did you get a syntax error (and if so, what was it?) Did your program
crash? Enter an infinite loop? *What happened*?

Please read the Posting Guidelines for this group. Then post a
short-but-complete example that we can run by copy and pasting, which
demonstrates your supposed error.

There is nothing wrong with that statement you have typed above.
Therefore, you have misdiagnosed your problem or mis-retyped your
actual code. This is why you are encouraged to create a
short-but-complete program in preparation for posting here - doing so
very often helps you find your *real* problem.

Now, all that being said, I have to ask how this need arose in the
first place? I've seen lots of bad code where people attempt to create
a string composed of the elements of an array separated by a comma, and
do so by concatenating each element of the array with a comma, and then
remove the final comma, like so:

my $string;
foreach my $elem (@array) {
$string .= "$elem,";
}
$string =~ s/,$//;
print "Values: $string\n";

If you are doing that, please stop. Instead, look up:

perldoc -f join
and
perldoc perlop (search for the $, and $" variables)

my $string = join(',', @array);
print "Values: $string\n";
# .... or....
{
local $, = ',';
print 'Values: ';
print @array;
print "\n";
}
#..... or ...
{
local $" = ',';
print "Values: @array\n";
}

Paul Lalli

 
Reply With Quote
 
 
 
 
Jürgen Exner
Guest
Posts: n/a
 
      01-30-2006
Jame Pearl wrote:
> How can I remove any trailing commas from the end of $var?
>
> I tried using
>
> $var =~ s/,+$//
>
> but it didn't work.


Works for me:

C:\tmp>type t.pl
use strict; use warnings;
my $var = 'foo,,,,';
print "$var\n";
$var =~ s/,+$//;
print "$var\n";
C:\tmp>t.pl
foo,,,,
foo

Maybe your $var does not contain what you think it contains?

jue


 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-30-2006
Jame Pearl schreef:

> How can I remove any trailing commas from the end of $var?
> I tried using
> $var =~ s/,+$//
> but it didn't work.


If

$var =~ s/,+$//m;

does work, see `perldoc perlre` again.

--
Affijn, Ruud

"Gewoon is een tijger."
 
Reply With Quote
 
Glenn Jackman
Guest
Posts: n/a
 
      01-30-2006
At 2006-01-30 06:16AM, Jame Pearl <(E-Mail Removed)> wrote:
> How can I remove any trailing commas from the end of $var?
>
> I tried using
>
> $var =~ s/,+$//
>
> but it didn't work.


Perhaps $var contains trailing whitespace. Try:
$var =~ s/,+\s+$//;



--
Glenn Jackman
Ulterior Designer
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-30-2006
Abigail <(E-Mail Removed)> wrote in
news:(E-Mail Removed):

> Dr.Ruud ((E-Mail Removed)) wrote on MMMMDXXXV September MCMXCIII
> in <URL:news:(E-Mail Removed)>:
> ^^ Jame Pearl schreef:
> ^^
> ^^ > How can I remove any trailing commas from the end of $var?
> ^^ > I tried using
> ^^ > $var =~ s/,+$//
> ^^ > but it didn't work.
> ^^
> ^^ If
> ^^
> ^^ $var =~ s/,+$//m;
> ^^
> ^^ does work, see `perldoc perlre` again.
>
>
> Hmmm.
>
>
> $ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
> foo
> bar,


This seals it for me. I have now been converted to the PBP recommendation
precisely which I can never remember what m and s modifiers do:

#!/usr/bin/perl

use strict;
use warnings;

my $var = "foo,\nbar,";
$var =~ s{ ,\z }{ }msx;
print "$var\n";

__END__

C:\Home\asu1\Perl> perl t.pl
foo,
bar
 
Reply With Quote
 
Dr.Ruud
Guest
Posts: n/a
 
      01-30-2006
Abigail schreef:
> Dr.Ruud:
>> Jame Pearl:


>>> How can I remove any trailing commas from the end of $var?
>>> I tried using
>>> $var =~ s/,+$//
>>> but it didn't work.

>>
>> If
>> $var =~ s/,+$//m;
>> does work, see `perldoc perlre` again.

>
> Hmmm.
>
> $ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
> foo
> bar,
>
> That doesn't remove the comma at "the end".


If

$var =~ s/,+$//gm;

does even work better, then Jame's data is complexer than I was prepared
to assume.

Next episode:

$var =~ s/,+(?=\s*$)//;

(which also removes more than "trailing commas")


And s/,+(?=\s*)$// isn't the same, see perlre:
"two zero-width assertions next to each other work
as though they're ANDed together"

--
Affijn, Ruud

"Gewoon is een tijger."

 
Reply With Quote
 
DJ Stunks
Guest
Posts: n/a
 
      01-30-2006
A. Sinan Unur wrote:
> This seals it for me. I have now been converted to the PBP recommendation
>


The undead legions of Damian Conway's Perl Best Hacker Army welcome
you.

The rest of you be forewarned: resistance is futile; you will be
assimilated.

PBH #12011
-jp

 
Reply With Quote
 
Uri Guttman
Guest
Posts: n/a
 
      01-30-2006
>>>>> "DS" == DJ Stunks <(E-Mail Removed)> writes:

DS> A. Sinan Unur wrote:
>> This seals it for me. I have now been converted to the PBP recommendation
>>


DS> The undead legions of Damian Conway's Perl Best Hacker Army welcome
DS> you.

DS> The rest of you be forewarned: resistance is futile; you will be
DS> assimilated.

DS> PBH #12011
DS> -jp

i won't drink all of his koolaid! and i was a tech reviewer of
PBP. just that some of his ideas are for some people and not for
me. using \z is good and i will try to make that a habit but enabling /m
and /s all the time just bothers me as i like don't like to do things
with no technical purpose like that. i know what they mean just fine and
use them when i need to.

an important aspect of PBP is choosing which ideas will work for you and
then being consistant about using them. that is something i support and
practice.

uri

--
Uri Guttman ------ http://www.velocityreviews.com/forums/(E-Mail Removed) -------- http://www.stemsystems.com
--Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
 
Reply With Quote
 
ced@carios2.ca.boeing.com
Guest
Posts: n/a
 
      01-30-2006

> > ...
> > $ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
> > foo
> > bar,

>
> This seals it for me. I have now been converted to the PBP recommendation
> precisely which I can never remember what m and s modifiers do:
>
> #!/usr/bin/perl
>
> use strict;
> use warnings;
>
> my $var = "foo,\nbar,";
> $var =~ s{ ,\z }{ }msx;
> print "$var\n";
>
> __END__
>
> C:\Home\asu1\Perl> perl t.pl
> foo,
> bar


The OP didn't indicate whether $var might have a trailing newline
after the comma or not but you could handle both cases:


$var =~ s{ , (?: \z | \Z) }{ }msx;

--
Charles DeRykus

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Remove only TRAILING whitespace Bob Smyph Ruby 4 10-14-2008 06:56 PM
Remove trailing spaces in ecilpse pd Java 3 12-07-2007 12:32 PM
Remove trailing space in Open Office spreadsheets? Evan Platt Computer Support 1 08-28-2006 09:05 PM
remove trailing whitespace from string Donald Canton C++ 5 02-09-2004 04:39 PM



Advertisments