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: Intermediate:
Deleting Spaces in variables

 



benchivers
Novice

Apr 21, 2002, 8:48 AM

Post #1 of 5 (527 views)
Deleting Spaces in variables Can't Post

Does anyone know how to delete spaces in variables?

e.g.

$variable = "Ben Chivers";

to make it:

$variable = "BenChivers";

any help would be most appreciated.



Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


Jasmine
Administrator

Apr 21, 2002, 9:17 AM

Post #2 of 5 (526 views)
Re: [benchivers] Deleting Spaces in variables [In reply to] Can't Post


Code
$name = "Ben Chivers"; 

$name =~ s/\s+//g;



uri
Thaumaturge

Apr 21, 2002, 4:48 PM

Post #3 of 5 (522 views)
Re: [benchivers] Deleting Spaces in variables [In reply to] Can't Post

\s matches any whitespace, not just real space chars. it is
not clear if the OP cared about the difference.

and tr/// is usually much faster than s/// for character oriented stuff.

Code
	tr/ //d ;	# delete only spaces 
tr/ \n\r\t//d ; # delete any whitespace



benchivers
Novice

Apr 21, 2002, 11:47 PM

Post #4 of 5 (517 views)
Re: [uri] Deleting Spaces in variables [In reply to] Can't Post

Thanks for both of your replies!

They both work fine.

Many Regards,

Ben Chivers
Wheres the damn coffee? zzzZZZZZ!!!


uri
Thaumaturge

Apr 22, 2002, 12:01 AM

Post #5 of 5 (514 views)
Re: [benchivers] Deleting Spaces in variables [In reply to] Can't Post

both work fine is not a proper answer in my book. do you understand how they are different and why you should choose
one over the other? many perl newbies learn s/// and neglect to learn tr///. and many others conflate the two just because the both can use the bind =~ operator. but if you are munging characters and not strings, then tr/// is likely to be faster and a better semantic match to the problem.

that last point bears repeating. code is NOT meant to get some program working but to express the solution to another coder. if code were just meant for computers, we would all use the same language. different languages and constructs map better onto certain problems and brains. the human aspect of code is much more important than the computer aspect. when you learn that, you become a much better coder.

in short, code is for people, not computers.

 
 


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

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