Gossamer Forum
Home : General : Perl Programming :

Replacing A Character

Quote Reply
Replacing A Character
Example:

The following variables are brought in from a database:

$name = 'John+Doe';
$street = '123+Your+Street'
$city = 'Fort+Lauderdale'

how do I write a script so that the + is replaced with a space for each of the variables and placed back in the same variables so that:

$name = 'John Doe';
$street = '123 Your Street'
$city = 'Fort Lauderdale'
Quote Reply
Re: Replacing A Character In reply to
$name =~ s/+/ /g;
$street =~ s/+/ /g;
$city =~ s/+/ /g;

What that is telling you is, take the contents of the variable, and substitute ("s")the "+" with a space, and do that for all occurances (the "g")

Smile

--Mark


------------------
You can reach my by ICQ at UID# 8602162

Quote Reply
Re: Replacing A Character In reply to
Mark:

Thank you for not only helping, but explaining how it works ... that is the part that really helps me.

However, it did not work. First I tried it as you have it (without quote marks around the expression) and got an error.

So I placed it in quotes using this as an example:

$name = 'John+Doe';
$name =~ 's/+/ /g';

that returns:

John+Doe

What am I doing wrong?

------------------
Quote Reply
Re: Replacing A Character In reply to
you definatly don't need quotes around it. it should be written as:

$name =~ s/+/ /g;

what error message was happening when you attempted that. also, are you doing this in the template files or in a .cgi file?

--Mark

------------------
You can reach my by ICQ at UID# 8602162

Quote Reply
Re: Replacing A Character In reply to
OK Mark:

My fault, I should have provided more detailed information. The application I am writing is a CGI program unrelated to Links or DBMan.

Here is a portion of my variable code:

dbmopen(%DBASE, "$DBM",0600);
$_ = $DBASE{$USER};
%RECORD = map split(/;/), split(/=/);
$us = $RECORD{'us'};
$hint = $RECORD{'hint'};
$first_name = $RECORD{'first_name'};
$last_name = $RECORD{'last_name'};
$street1 = $RECORD{'street1'};
$street1 =~ s/+/ /g;
$street2 = $RECORD{'street2'};

My test is on $street1 using the code you provided me. When the script is executed, I get the standard misconfiguration message:

Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator and inform them of the time the error occurred, and anything you might have done that may have caused the error.

Any clues? I am using a recent version of Perl ... v5.0004 I believe.
Quote Reply
Re: Replacing A Character In reply to
OH!!

My bad! Since "+" is a meta character that can be used in regular expressions, you need to escape it!

use

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

and you should be ok!

sorry bout that!

--mark
Quote Reply
Re: Replacing A Character In reply to
No reason to be sorry, Mark. You are being a big help ... and I am learning as we go! Smile

Well, I made the change as you directed, and I no longer get a misconfiguration error. However, now it truncates at the first existence of +. Example:

$street1 = '123+Uptown+Drive';
$street1 =~ s/\+/ /g;

returns: 123

If you have any last ideas they would be appreciated.
Quote Reply
Re: Replacing A Character In reply to
Sounds like it's something else in your program that's causing a truncation.

#!/usr/bin/perl

$street1 = '123+Uptown+Drive';
$street1 =~ s/\+/ /g;

print "$street1\n";

I run the above as a standalone program, and get:

123 Uptown Drive

(the 123 Uptown Drive is copy & pasted directly from my output screen in my telnet session).

--Mark

------------------
You can reach my by ICQ at UID# 8602162

Quote Reply
Re: Replacing A Character In reply to
Mark:

Thanks again for all your help. This is sooo weird. I tried replacing the '+' with other characters and it works fine. It is just when I try to use a space that it truncates the rest of the variable.

Is there a way to insert an ASCII code instead (032)? I'm getting desperate! Smile