CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Deleting first and last char in a string

 



yim11
Novice

Feb 7, 2001, 12:03 PM

Post #1 of 5 (470 views)
Deleting first and last char in a string Can't Post

Hello,
I have a string of numbers, and I need to strip the first and last numbers off the string.

For example: if my string were 1003045877663, I only need 00304587766. The first number in the string will always be a 1.

I have tried td/1//d; but that strips all 1's out of the string.

Any help or pointers to help me would be greatly appreciated!

TIA!
-jim




sleuth
Enthusiast

Feb 7, 2001, 1:00 PM

Post #2 of 5 (468 views)
Re: Deleting first and last char in a string [In reply to] Can't Post

$string =~ s!^\d(.*?)\d$!$1!;

Would Work.

Tony



yim11
Novice

Feb 7, 2001, 1:19 PM

Post #3 of 5 (467 views)
Re: Deleting first and last char in a string [In reply to] Can't Post

THANK YOU VERY MUCH!!!!

works like a charm!

if you have the time and patience, I would be interested in knowing more about what you did. I have been reading about s and the switches, but cannot seem to get a grasp on it.

Thanks again!
-jim



japhy
Enthusiast

Feb 7, 2001, 5:36 PM

Post #4 of 5 (461 views)
Re: Deleting first and last char in a string [In reply to] Can't Post

I'd suggest:


Code
$str =~ s/^.//s and chop $str;

Or at least


Code
$str =~ s/^.//s, $str =~ s/.$//s;

as a more efficient solution.

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author


sleuth
Enthusiast

Feb 7, 2001, 8:55 PM

Post #5 of 5 (456 views)
Re: Deleting first and last char in a string [In reply to] Can't Post

 Well, They're called regular expressions, and I couldn't explain it now since I am actually pretty tied up with deadlines, so I searched down a tutorial I think will help.

http://www.troubleshooters.com/codecorn/littperl/perlreg.htm

I recommend a book also,

Perl Core Language, Little Black Book" by Steven Holzner. It's $19.95, and it's a great book because it's layed out plainly and the regular expressions chapter is where I got started with regex.

Good luck Mate,

Japhy, I tried to make it a one step process is all.

Tony


 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives