more help with spaces, please

Okay so this works:

$location =~ s/ /%20/g;

if I take it out, it doesn't work (in Netscape, but IE is fine).

However, it is replacing a single space with three '%20'

So that what should be "new york" comes out as "new%20%20%20york"

The script works nonetheless, but I think it looks messy.

Anyone know why it's doing this, and how I can fix it?

Thank you.

Who is Participating?
TsvetomirConnect With a Mentor Commented:
now I sow your other post probably the scripts misses somthing and multiplicates the chars by 3 each time .. I can't tell you where exactly is the mistace becouse I can't see the script here ... but the last probably  will solve the problem.
Beacouse it maj be used somewhere else as hex encoded you can use

$location =~ s/\s+/%20/g;

if this doesn't solve the problem you shold post the script here ...
if it comes to url encoded string:

the standard doesn't allow to use space you should replace them with "+" sign or it's ascii number in hex.
space is ASCII 32 (in HEX it's  20) that's why it becomes %20.

this way you can encode other not allowed(or with special meaning as "/" ) chars - for example !@#$%^&*(),/:
  = ASCII 32 = %20
! = ASCII 33 = %21
" = ASCII 34 = %22
# = ASCII 35 = %23
and s.o. and s.o.

did you get the idea ?

P.S if you want you can strip the aditional spaces and to use  +  it's more readable:

$location = "New   York"
$location =~ s/\s+/+/g;

#now $location is "New+York"
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

celereAuthor Commented:
I will post the script shortly, but here is how it works thus far.

There is a menu of links with %20 hard coded in it:
<a ref'"">

This takes you to the "read.cgi" script, which delimits the :: as $shortname, $longname; opens up and reads $shortname.htm (newyork.htm), and places New York around the page to let the user know where they are.

$page =~ tr/ /%20/;
$newadtarget = "$page";

Then there is a "post to this page" button (href, not form)
<a href="$newadtarget">

which then goes to a second script for posting the messages. Here, NS is still reading only one %20 between New and York. After posting it returns to the same script for a "Thank You" page. This is the first occurence of the %20's tripling, as it reads "click here to see your ad" taking you back to <read.cgi..$shortname::$longname>

So I need $longname to be able to take one %20, not three, and I need it to be dynamic; I can't hardcode anything into it.

The message script also prints a link to the ad to appear among the "random message generator". It prints it with 3 %20's. So if someone were to click on that, and subsequently post an ad on the same page, it would print with 9 %20's.

I would like the most efficient way of doing this (keeping my scripts separate however, since they contain a lot of HTML). I will try your idea above, but if you think I should start over with the links, etc. and if you can help me clean it up (it is a little verbose!) I'll increase the points.

Thanks for pointing out I had two questions. I didn't post twice, I posted once and then "edited" it, but there should only be one.

It's not only spaces that are now allowed in URL, there are other characters too. Sample below should get them all.

use URI::Escape;
$_="New     York";
s/ +/ /;
print "$_\n";
ARE YOU shure its' like that ??? :
$page =~ tr/ /%20/;

this is a mistake ?!

it should be
$page =~ s/ /%20/g;
# or to have only 1 space:
$page =~ s/\s+/%20/;

tr replaces the chrars from left to their CORESPONDING chars on the rigth side 1 by 1 this means that
if it found space it will replace it with the coresponding char from right side and this is % not %20!

are u sure it's like that ??  I'm amazed that script works like that!

try this and see if it works ...

celereAuthor Commented:
I replaced all the spaces with +'s and now it works like a charm. Thank You!
you are wellcome :))
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.