Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Replacing underscore characters with whitespace

by markinoz (Initiate)
on Jun 13, 2002 at 09:55 UTC ( #174133=perlquestion: print w/ replies, xml ) Need Help??
markinoz has asked for the wisdom of the Perl Monks concerning the following question:

Hi

I have a survey script that I need to hack, and cant work it out.

There is a string called $FORM{'survey'}, this is inputted from a form and the script removes any whitespace so it can be passed thru a url ( with me so far?? )

The problem is when I return $survey it has the underscores in the survey name and my client would like them removed.

Is there a way I can replace the underscore chars with white spaces ( this isnt going to be passed to the url its just for a html table title. )

well actually I know there is a way to do this but I cant work it out

I have pasted some of the code at the bottom so maybe someone could point me in the right direction??

Thanks
Mark R

<TABLE BGCOLOR="$table_bgcolor" CELLSPACING="$table_cellspacing" CELLP +ADDING="$table_cellpadding" BORDER="$table_border" WIDTH="$table_widt +h"> <TR BGCOLOR="$table_caption_row_color"><TD COLSPAN=2> <FONT FACE=\"$font_type\" SIZE=\"$caption_font_size\" COLOR=\" +$table_font_caption_color\"><B> Online Surveys</B></FONT></TD></TR> !; opendir(SOFFICE,"$surveyoffice_base_dir") || &error("Unable to + read from Survey directory"); while($file = readdir(SOFFICE)) { if($file =~ /^(.*).info$/) { $survey = $1; if(-e "$surveyoffice_base_dir/$survey.rclosed" && -e "$surveyoffice_ba +se_dir/$survey.closed") { next; } print "<TR BGCOLOR=\"$table_row_color\"><TD><FONT FACE +=\"$font_type\" SIZE=\"$font_size\" COLOR=\"$table_font_color\">$surv +ey</FONT></TD><TD>\n"; if(! -e "$surveyoffice_base_dir/$survey.rclosed") { print "<A HREF=\"survey.cgi?action=view&survey=$su +rvey\"><font face=\"arial\" size=\"1\">View Results</A>\n"; } else { print "<font face=\"arial\" size=\"1\">Results Not + Available\n"; }

as you can see $survey is used for the table title ( html ) and also used to pass the address to the script thru the url.

I was hoping to make a copy of $survey ( $survey1??? ) and remove the _ chars and use $survey1 for my html title.

I have tried hardcoding $survey1 = "title here" and it worked fine

all I need to know is how to duplicate the $survey

Thanks Again

Edited 2002-06-13 by mirod: changed the title (was: URGTENT: Need to remove undescore characters) and added <p> tags

Comment on Replacing underscore characters with whitespace
Download Code
Re: Replacing undescore characters with whitespace
by cjf (Parson) on Jun 13, 2002 at 11:31 UTC

    Some introductory reading...

    Also,you should consider a templating system such as HTML::Template (or at very least some heredocs).

    Update:

    this is inputted from a form

    Are you using CGI.pm to parse the input? I assume you have taint mode enabled and are checking all the parameters (especially ones like $surveyoffice_base_dir), right? If not, read this.

    Another Update: reading up on s/// might help you out a bit as well. Please look into the above suggestions first though, they are very important.

Re: URGTENT: Need to remove undescore characters
by Abigail-II (Bishop) on Jun 13, 2002 at 12:13 UTC
    Is there a way I can replace the underscore chars with white spaces
    y -_- -
      This code may work, but not reliably -- it depends on your background colour. For comparison,
      y -_- -
      will never work (on most browsers), as it will tend to generate pinkspace. Of course, Unicode probably has 4 different solutions for you (although 2 will not work for Japanese and the other 2 fail for Arabic). Perl supports Unicode, so you should have no problems in that regard.

      I suspect, however, that this is an inherent problem; Perl cannot help you here.

Re: URGTENT: Need to remove undescore characters
by 914 (Pilgrim) on Jun 13, 2002 at 14:45 UTC
    You really should do the reading the others suggested, and CGI.pm and taint mode are a good idea too.

    How embarassed will you be if your script allows someone to crack the machine and use it as a kidporn or war3z trading server?

    Anyhow, just so you can fix your current "URGTENT" urgent problem (and right after that, read the links provided by the good monks).

    $variable =~ s/_/ /g;
    translated, that's "variable becomes substitute /(underscore)/ with /(space)/ globally
      Hi thanks for your help. I didnt write the survey script, Im only trying to hack it for aesthetics sake. My client didnt like how the title looked with _ chars in it. Anyway I added the code you suggested ( I use seach and replace every day thru command line for line breaks etc ) so maybe I didnt explain myself correctly, I knew how to do the replace just wasnt sure of the structure for it. Again thanks and heres what I added
      $String = "$survey"; $String =~ s/_/ /g;
      then called $string instead of $survey in the html title simple huh?? see you around and thanks for everyone who posted. p.s. when the boss gives me a few minutes to breath Ill read the links as suggested. Mark R
        You're welcome... it's good to see people learn, to be honest i often have to look up the most basic things myself.

        One minor hint:
        When you want to assign the value of one variable to another, you needn't quote the original variable.

        Hence,

        $String = "$survey";
        is actually better written as:
        $String = $survey;
        Perl is flexible enough to live with the first version, but that ability was really meant more for things like this:
        if ($something) { # do some stuff } else { # there was an error, set it $error = "informative error message"; $error_num = 45; } print STDERR "Error $error_num $error";
        This will (if there was an error, ie $something was not true) produce:
        Error 45    informative error message on the STDERR output (or in a logfile, or wherever you printed the error)

        In any case, please do take a look at those other links in this thread, if you read/heed them, you'll actually get better answers much more quickly at this site.

        have a great day!

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://174133]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2014-08-23 00:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (168 votes), past polls