J
Jame Pearl
How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
I tried using
$var =~ s/,+$//
but it didn't work.
Jame said:How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
Jame said:How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
At 2006-01-30 06:16AM said:How can I remove any trailing commas from the end of $var?
I tried using
$var =~ s/,+$//
but it didn't work.
Dr.Ruud ([email protected]) wrote on MMMMDXXXV September MCMXCIII
in <URL:^^ 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,
Dr.Ruud:
Hmmm.
$ perl -wle '$var = "foo,\nbar,"; $var =~ s/,$//m; print $var'
foo
bar,
That doesn't remove the comma at "the end".
A. Sinan Unur said:This seals it for me. I have now been converted to the PBP recommendation
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
Abigail said:Uri Guttman ([email protected]) wrote on MMMMDXXXV September MCMXCIII
in <URL:~~
~~ 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.
I think that writing
s{ ,\z }{}xms
if you mean
s/,$//
is borderline idiocy.
If I wanted to be needlessly verbose, I would have written it in Java.
Abigail said:(e-mail address removed) ([email protected]) wrote on MMMMDXXXV
September MCMXCIII in <URL:""
"" > > ...
"" > > $ 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;
Why so difficult? $ already does that:
$ perl -wle '$var = "foo,"; $var =~ s/,$//; print "[$var]"'
[foo]
$ perl -wle '$var = "foo,\n"; $var =~ s/,$//; print "[$var]"'
[foo
]
And one of \z or \Z does that as well, matching at the end of the string,
or just before a final newline. But I can never remember which one does
so. $ OTOH, I won't forget.
Also note that your line replaces the comma with four spaces - not quite
the same as just removing the comma.
I see this mistake being made over and over again. /x only influences the
whitespace in the _pattern_. Whitespace in the replacement part remains
whitespace.
Abigail wrote:
....
Yes, thanks. I mistakenly thought I saw a space in Sinan's
replacement { } and figured he couldn't be wrong....![]()
Uri Guttman ([email protected]) wrote on MMMMDXXXV September
MCMXCIII in <URL:~~
~~ 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.
I think that writing
s{ ,\z }{}xms
if you mean
s/,$//
is borderline idiocy.
If I wanted to be needlessly verbose, I would have written it in Java.
;-)
Besides being concise, 's/,$//' has the advantage it works in 'sed'
and 'vi' as well. 's{ ,\z }{}xms' does not.
Abigail said:(e-mail address removed) ([email protected]) wrote on MMMMDXXXV
September MCMXCIII in <URL:
I was thinking of a newline just before a trailing comma:
perl -e '$_ = "foo,\n,"; s{ , ( \z | \Z) }{}msx'' # result:
"foo,\n"
The alternation (\z|\Z) strips the trailing comma in "foo,\n," and
would strip
the comma in just "foo,\n" as well.
But \Z does that as well:
$ perl -e '$_ = "foo,\n"; s{ ,\Z }{}msx;' # result: "foo\n"
$ perl -e '$_ = "foo,\n,"; s{ ,\Z }{}msx;' # result: "foo,\n"
whereas:
$ perl -e '$_ = "foo,\n,"; s{,$}{}msx' # result: "foo\n,"
only picks up the leading comma.
Yeah, but the problem is the misuse of /m. Don't.
$ perl -e '$_ = "foo,\n"; s/,$//;' # result: "foo\n"
$ perl -e '$_ = "foo,\n,"; s/,$//;' # result: "foo,\n"
If you keep things simple, it just works.
I meant (\z|\Z) of course....
I still believe (\s|\S) -- without the unnecessary /ms -- is a
^^^^^
A. Sinan Unur said: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:
Uri Guttman said: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.
Abigail said:(e-mail address removed) ([email protected]) wrote on
MMMMDXXXVII September MCMXCIII in <URL:-:
-: I still believe (\s|\S) -- without the unnecessary /ms -- is a
-: useful idiom here
-: to guarantee removal of the trailing character.
[You mean (\z|\Z) here - as pointed out].
I disagree. I do not see the point of (\z|\Z). I don't think there's anything
that is matched by \z that isn't matched by \Z.
Could you give an example of a match (or substitution) where using '(\z|\Z)'
gives a different result from using '\Z'?
Your're right. \z just does half of what \Z does.
NoSomehow I conflated \Z with something that matched only
before
a trailing newline. I've got it straight now..
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.