CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Substitution question



Aug 25, 2000, 4:33 PM

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

I'm trying to write a short script to let participants at my site write user profiles for themselves. The problem I am running into now is in substituting an underscore (_) for each blank space in multi-word nicknames.

I am doing this in order to name the files that are created, for example I want a user with the nickname Multi Word Nick's profile to be written to an html file named Multi_Word_Nick.html

Here is the code I am using right now to create a $filename variable from the $name variable they enter in my form:

$filename = $name;
$filename = $filename =~ s/ /_/g;

My problem is that a $name of "Multi Word Nick" is given a file name like 2.html, 3.html, etc instead of Multi_Word_Nick.html

Can anybody spot what I am doing wrong and offer any suggestions? Thanks.

User / Moderator

Aug 25, 2000, 5:23 PM

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

Your counting the number of substitutions s///g is making, hence the 1.html, 2.html, etc.

You'll get the functionality you want by omitting the extra assignment to $filename as the binding operator (=~) usually applies its changes to the variable it's used on.<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

$filename = $name;
$filename =~ s/ /_/g;
# --- You were doing this ... ---
# $count = $filename =~ s/ /_/g;</pre><HR></BLOCKQUOTE>
Alternately, you can do the same as a one-liner ...<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

($filename = $name) =~ s/ /_/g;</pre><HR></BLOCKQUOTE>


Aug 26, 2000, 11:41 AM

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

That did it, thanks Kanji.


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

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