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:
Substitution

 



Saya
Novice

Jul 5, 2002, 12:53 AM

Post #1 of 3 (363 views)
Substitution Can't Post

Hi,

Scenario:

$test = "My name is Peter from Petersburg"

$test1 = "My name is Pet-from Petersburg"

$test2 = "My name-is Peter from Petersburg"

I need some substition function that can take the variables from above and apply following rules. If the length is greater than 16 then, break the string into 2. The $first variable consisting of the first 16 characters. If there is an "-" in the variable then add an "\n" right after the "-". If there isn't "-" present then add a "\n" after the last whitespace, but if there is a "-" present but there is a whitespace after the "-" then add "\n" after the whitespace.

So the variable from above should look like this after being processed:

$test = "My name is\n Peter from Petersburg"

$test1 = "My name is Pet-\nfrom Petersburg"

$test2 = "My name-is\n Peter from Petersburg"

:-)


davorg
Thaumaturge / Moderator

Jul 5, 2002, 1:02 AM

Post #2 of 3 (362 views)
Re: [Saya] Substitution [In reply to] Can't Post

This is starting to sound a bit like you're expecting us to do your homework for you - so I'm just going to point you at some documentation.

The feature that you need to use is called "regular expressions". The definitive documentation on regexes is at perldoc perlre but it can be a bit heavy going for beginners. The forthcoming version of Perl (5.8.0) has added two new pieces of regex documentation that are aimed more at beginners so you might want to look at perldoc perlretut and perldoc perlrequick

--
Dave Cross, Perl Hacker, Trainer and Writer
http://www.dave.org.uk/
Get more help at Perl Monks


Saya
Novice

Jul 5, 2002, 1:07 AM

Post #3 of 3 (360 views)
Re: [Saya] Substitution [In reply to] Can't Post

No it's not like that, it's just that I have never worked with regular expressions before and I had an assignment to hand in. Anyhows I'll keep trying to solve it. Thanks for your help anyway.

 
 


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

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