> $string =~ s/^.(.*).$/$1/;

It's prettier, but it's very inefficient.  It's probably 20+ times slower
then doing it in two steps.

It has to do with how Perl internally handled string data.  When you s/// or
substr() all it does is move a pointer.  With your version it would need to
rewrite the whole string value, which is pretty expensive in comparison.

Rob

-----Original Message-----
From: James Edward Gray II [mailto:[EMAIL PROTECTED]
Sent: Tuesday, October 07, 2003 6:07 PM
To: Li, William
Cc: [EMAIL PROTECTED]
Subject: Re: Chopping off first&last character in a string


On Tuesday, October 7, 2003, at 11:56  AM, Li, William wrote:

> Hi,

Howdy.

> Silly question, is there an equal but opposite function to chop in 
> Perl?
> I'd like to remove the first and last character of a string and am 
> looking
> for a simple way to do it.  So far I have:
>
> # "1234567" -> "23456"
> chop($string); # "123456"
> $string = reverse($string); # "654321"
> chop($string); # "65432"
> $string = reverse($string); # "23456"

How about an all-in-one try:

$string =~ s/^.(.*).$/$1/;

James


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to