Gossamer Forum
Home : General : Perl Programming :

Chopping white space out... how?

Quote Reply
Chopping white space out... how?
Ok, My problem is this little white space at the end of my variables... I need to figure out howto get rid of it, how would I through pattern matching?

The variable with the whitespace at the end is for displaying a graphic.. so I have:

Code:
<img src="blah_3000 .gif">
/\
(Space)

IE doesn't mind the space, it still displays the images... It's netscape that doesn't like them.. I've also created doubles of each graphic to make sure, by renaming the gfx to 'blah_3000 . gif'

How would I chop the whitespace from lets say:
Code:
$blah = "blah_3000 "

Cuz what I do is append the .gif to it...
Code:
$gfx = $blah . ".gif";

so the problem is in $blah right away...

Any help would be appreciated.

Thanks...
--Jacob
jacob@123webmaster.com

Quote Reply
Re: Chopping white space out... how? In reply to
IF you know there is a trailing space every time, find out why Smile It's better to get rid of the source of the errror.

If there is _always_ a trailing space, use chop($variable) and it will remove the last character.

or s/\s+//g is the pattern to turn any amount (one or more consecutive) of white spaces to nothing in a string.

$variable =~ s/\s+//g

Forgot... the best thing would be to create your file name, then regex it. Might as well do it at the END of the process, and eliminate any other source of error.



[This message has been edited by pugdog (edited July 16, 1999).]
Quote Reply
Re: Chopping white space out... how? In reply to
Ok, Thanks! It worked!

--Jacob
jacob@123webmaster.com
Quote Reply
Re: [pugdog] Chopping white space out... how? In reply to
Very old post but I do have a similar problem. Have a PHP version of this but need cgi version.
Job at hand. have a script that creates meta tags - these are for a cut 'n paste into another script for creating web bages. Problem being the other script has a problem handling white space and as soon as it encounters it - anything after the first white space is ignored totally and not entered into the database.
Therefore, as was suggested by pugdog, need to get rid of the problem at the source.

I get a 500 error when I put this in the script..: $variable =~ s/\s+//g

What exactly would do this. All help appreciated.
Quote Reply
Re: [Gypsypup] Chopping white space out... how? In reply to
Code:
$variable =~ s/\s+//g;

$variable needs to hold the value that you are trying to edit (i.e if the variable was $test, you would use $test =~ xxx ... etc).

Also, be sure you have the ; at the end of the line.. otherwise that will cause a 500 IS Error Wink

Cheers

Andy (mod)
andy@ultranerds.co.uk
Want to give me something back for my help? Please see my Amazon Wish List
GLinks ULTRA Package | GLinks ULTRA Package PRO
Links SQL Plugins | Website Design and SEO | UltraNerds | ULTRAGLobals Plugin | Pre-Made Template Sets | FREE GLinks Plugins!