http://qs321.pair.com?node_id=76238

belize has asked for the wisdom of the Perl Monks concerning the following question:

I am submitting a form that will replace matching fields in a template file with what was submitted.

The template file takes this form:

LABEL1: [field1] LABEL2: [field2] LABEL3: [field3]

I open the template file, read each line into an array, then start working on each line as follows:

foreach $File (@File) { $File =~ s/\n//; foreach $Field (@InputFields) { $ReplaceVar = $FORM($Field); $SearchVar = '\[' . Field . '\]'; $File =~ s/$SearchVar/$ReplaceVar/g; } print "$File\n"; }

QUESTIONS:
1. If someone does not fill Field1 in the form, the printout will show " LABEL: [Field1] ". How do I get rid of this. Do I first run the above, then run it again searching " '\[.*\]' " and replacing it with ""? I tried this but running it a second time erased all the fields.

2. What does " $File =~ s/\n//; " accomplish?

Thanks!

Replies are listed 'Best First'.
Re: Pattern Match and Replace Problem
by Chady (Priest) on Apr 27, 2001 at 23:51 UTC

    Answer to #2:

    you already split the individual lines into array fields, so \ns will only be at the end of the lines... you should chomp instead:

    $File =~ s/\n//; # removes newline characters. # it should be chomp($File); # removes newline characters from the end of the line fa +ster.


    He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

    Chady | http://chady.net/
Re: Pattern Match and Replace Problem
by belize (Deacon) on Apr 27, 2001 at 23:50 UTC
    Will this work:

    foreach $File (@File) { $File =~ s/\n//; foreach $Field (@InputFields) { $ReplaceVar = $FORM($Field); $ReplaceVar2 = ''; $SearchVar = '\[' . Field . '\]'; $SearchVar2 = '\['.*'\]'; $File =~ s/$SearchVar/$ReplaceVar/g; $File =~ s/$SearchVar2/$ReplaceVar2/g; } print "$File\n"; }

    Can I run a search and replace on a line, then run a second one consecutively to catch the ones that weren't replaced. Doesn't seem to work for me? Why not?

      How are you recieving your form data?? I assume with CGI.pm ??

      If that's the case... try this when you read the data:

      use CGI; $q = new CGI; $Field1 = $q->param('Field1) ? $q->param('Field1') : ' '; $Field2 = $q->param('Field2) ? $q->param('Field2') : ' '; # and so on...

      This way, if someone didn't fill in the "Field1" it will be replaced by a space character... or whatever you specify.


      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady | http://chady.net/
Re: Pattern Match and Replace Problem
by little (Curate) on Apr 27, 2001 at 23:32 UTC
    1.assign a default value alike   or ' ' to the fields in your scipt (so they are lets say values in an arry and get overwritten with the content from the from , but only if there is one), so they get a content anyway.
    2. shall remove the newlines from $File, but its an expensive way, that seems to be done to process the content of $File like if it was only one long line, probably to find matches over multiple lines
    Have a nice day
    All decision is left to your taste
      Sorry, I should of asked why do the new lines have to be removed before doing tha pattern matching?