How can I remove trailing commas?

A

axel

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'?

For single line strings it is usually a good idea to chomp them as
soon as they have been read in from an external data source and
then only explicitly use a separate newline when it comes to the
output stage - it avoids all sorts of problems.

Axel
 
C

ced

Abigail said:
(e-mail address removed) ([email protected]) wrote on
MMMMDXXXVIII September MCMXCIII in <URL:$$
$$ (e-mail address removed) wrote:
$$ > Abigail wrote:
$$ > > ...
$$ > > 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.
$$ >
$$ > Your're right. \z just does half of what \Z does.
$$ > >
$$ > > Could you give an example of a match (or substitution) where using '(\z|\Z)'
$$ > > gives a different result from using '\Z'?
$$ > >
$$ >
$$ > No :) Somehow I conflated \Z with something that matched only
$$ > before
$$ > a trailing newline. I've got it straight now..
$$
$$ A bit far fetched perhaps but, if you had a burning need to know where
$$ the strip occurred:
$$
$$
$$ if ( s{ , (?: (\z) | (\Z) ) }{}x ) {
$$ print "stripped comma ", defined $1 ? "from end of string"
$$ : "before
$$ trailing newline";
$$ }


Well, that's '((\z)|(\Z))', not '(\z|\Z)'.
Huh?

But if I wanted to know, I'd write that as:

if (s/,(\n)?\z/) {
print "stripped comma ", defined $1 ? "before end of string"
: "from end of string";
}

I think you intended something a bit different :) The RHS of 's'
is missing....

The (\n)? can't be right either since, if present, a RHS // would then
remove
the \n too. So I assume you meant thiis to be something entirely
different...


I could shorten mine to: s{ , (\z)? \Z }{}x though which seems
hard
to beat.
 

Ask a Question

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.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top