For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > August 2007 > How do I truncate or remove a trailing character









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How do I truncate or remove a trailing character
Bret Goodfellow

2007-08-01, 7:00 pm

Okay, I know this has to be simple, but because I am trying to truncate
or remove a special character I've run into a roadblock. Here's what I
want to do.

$value = "12345)" ;

How do I change $value so that the trailing ")" is removed. In
otherwords with the given example, how do I manipulate $value so that
its value is "12345"? Please keep in mind that $value may be variable
in length, so I can't use substr($value, 0, 5). Can split be used to do
this? Stumped.

Wagner, David --- Senior Programmer Analyst --- WG

2007-08-01, 7:00 pm

> -----Original Message-----
> From: Bret Goodfellow [mailto:Bret.Goodfellow@questar.com]=20
> Sent: Wednesday, August 01, 2007 14:25
> To: beginners@perl.org
> Subject: How do I truncate or remove a trailing character
>=20
> Okay, I know this has to be simple, but because I am trying=20
> to truncate
> or remove a special character I've run into a roadblock. =20
> Here's what I
> want to do. =20
> =20
> $value =3D "12345)" ;
> =20
> How do I change $value so that the trailing ")" is removed. In
> otherwords with the given example, how do I manipulate $value so that
> its value is "12345"? Please keep in mind that $value may be variable
> in length, so I can't use substr($value, 0, 5). Can split be=20
> used to do
> this? Stumped.
>=20

Will there always be non decimal number or it could be easier to
pull all the numbers:
if ( $value =3D~ /^(\d+)/ ) {
$value =3D $1
}
else {
printf "Expecting a numeric field, but got<%s>\n", $value;
# now either back to the top of a loop or whatever

}
or if always want last character removed:
chop($value); # removes last character
Or
$value =3D substr($value,0,length($value)-1);

You have to decide what edits or the data looks like, but something to
think about.

Wags ;)

****************************************
******************************
This message contains information that is confidential and proprietary to F=
edEx Freight or its affiliates. It is intended only for the recipient name=
d and for the express purpose(s) described therein. Any other use is proh=
ibited.
****************************************
******************************

Rob Dixon

2007-08-01, 7:00 pm

Wagner, David --- Senior Programmer Analyst --- WGO wrote:
>
> From: Bret Goodfellow [mailto:Bret.Goodfellow@questar.com]
> Will there always be non decimal number or it could be easier to pull
> all the numbers:
>
> if ( $value =~ /^(\d+)/ ) {
> $value = $1
> }
> else {
> printf "Expecting a numeric field, but got<%s>\n", $value;
> # now either back to the top of a loop or whatever
>
> }
> or if always want last character removed:
> chop($value); # removes last character
> Or
> $value = substr($value,0,length($value)-1);


Or

substr($value, -1, 1) = '';

Rob
John W. Krahn

2007-08-01, 10:00 pm

Bret Goodfellow wrote:
> Okay, I know this has to be simple, but because I am trying to truncate
> or remove a special character I've run into a roadblock. Here's what I
> want to do.
>
> $value = "12345)" ;
>
> How do I change $value so that the trailing ")" is removed. In
> otherwords with the given example, how do I manipulate $value so that
> its value is "12345"? Please keep in mind that $value may be variable
> in length, so I can't use substr($value, 0, 5). Can split be used to do
> this? Stumped.


$value =~ s/\D+//g;

Or:

$value =~ tr/0-9//cd;


John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order. -- Larry Wall
Chas Owens

2007-08-02, 7:00 pm

On 8/1/07, Bret Goodfellow <Bret.Goodfellow@questar.com> wrote:
> Okay, I know this has to be simple, but because I am trying to truncate
> or remove a special character I've run into a roadblock. Here's what I
> want to do.
>
> $value = "12345)" ;
>
> How do I change $value so that the trailing ")" is removed. In
> otherwords with the given example, how do I manipulate $value so that
> its value is "12345"? Please keep in mind that $value may be variable
> in length, so I can't use substr($value, 0, 5). Can split be used to do
> this? Stumped.


There are many ways to do it. Which one is best for your situation
depends a lot on your data, its expected values, and how you are going
to use it.

If you always want to remove the last character:
my $last_char = chop $value;

if you want to remove the last character if it is a certain character or string:
{ local $/ = $string_to_remove; chomp $value }
or
#note: instead of \Q$string_to_remove\e this can be a regex pattern
#this is much more useful (if slower) than the chomp
$value =~ s/(.*)\Q$string_to_remove\e/$1/;

If you and to remove all non-number characters:
$value =~ s/\D+//g;
or
$value =~ tr/0-9//cd;

if you want only the numbers at the start of the string:
#note: if there are no numbers at the start $value will be ''
$value =~ s/^(\d*)/$1/;
or
#note: if there are no numbers at the start* $value will be zero
$value += 0;


* in this case start is defined as ^\s*
Paul Lalli

2007-08-02, 7:00 pm

On Aug 2, 12:11 pm, chas.ow...@gmail.com (Chas Owens) wrote:
> On 8/1/07, Bret Goodfellow <Bret.Goodfel...@questar.com> wrote:
>
>
>
[color=darkred]
> if you want only the numbers at the start of the string:
> #note: if there are no numbers at the start $value will be ''
> $value =~ s/^(\d*)/$1/;


Replacing the a match that's entirely captured with $1 is a no-op.

I think you meant:
$value =~ s/^(\d*).*/$1/;
or
$value =~ /^(\d*)/ and $value = $1;

> or
> #note: if there are no numbers at the start* $value will be zero
> $value += 0;


This will cause a warning to be displayed if warnings are properly
enabled. Locally disable the warning with:
{
no warnings 'numeric';
$value += 0;
}

Paul Lalli

Perl Pra

2007-08-04, 7:58 am

Hi

Please try the following


___________________________ BEGIN
$value = "12345)" ;

chop $value;

print "$value";

_______________ END

On 8/2/07, Bret Goodfellow <Bret.Goodfellow@questar.com> wrote:
>
> Okay, I know this has to be simple, but because I am trying to truncate
> or remove a special character I've run into a roadblock. Here's what I
> want to do.
>
> $value = "12345)" ;
>
> How do I change $value so that the trailing ")" is removed. In
> otherwords with the given example, how do I manipulate $value so that
> its value is "12345"? Please keep in mind that $value may be variable
> in length, so I can't use substr($value, 0, 5). Can split be used to do
> this? Stumped.
>


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com