On 8/1/07, Bret Goodfellow <[EMAIL PROTECTED]> 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*
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/