[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Q on Substitution improvement



>>>>> "Walter" == Walter Torres <walter@tscinternet.com> writes:

Walter> I have this...
Walter> # ---------------------------------------------------------
Walter> # We are going to run through each name/value pair sent up and
Walter> # parse it to replace any RETURN (cr/lf) with <br>, the HTML line
Walter> # break code and quotes with '&quot;'.
Walter> for (keys %cgiNameValues)
Walter> {
Walter>    $cgiNameValues{$_} =~ s/\n/<br>/g;      # change RETURNS to <br>
Walter>    $cgiNameValues{$_} =~ s/\"/\&quot;/g;   # Change quotes to HTML code
Walter> }

Walter> OK, it works, but it doesn't feel right.

Walter> I also have a list of 50+ such swaps I need to make and I really don't think
Walter> I need to create 50 such lines.

Walter> Isn't there a Perl-ish method to run down my 50+ hash and do the swap?

Walter> Thanks

This is a typical way of handling such:

    $map{'"'} = "&quot;";
    $map{"\n"} = "<br>";
    ...
    my $pat = "(" . join("|", map quotemeta $_, reverse sort keys %map), ")";
    eval q{$pat = qr/$pat/} if $] >= 5.005;
    s/$pat/$map{$1}/g;

Even handles if you have "\n\n" and "\n";  Can't have any patterns though.

-- 
Name: Randal L. Schwartz / Stonehenge Consulting Services (503)777-0095
Keywords: Perl training, UNIX[tm] consulting, video production, skiing, flying
Email: <merlyn@stonehenge.com> Snail: (Call) PGP-Key: (finger merlyn@teleport.com)
Web: <A HREF="http://www.stonehenge.com/merlyn/">My Home Page!</A>
Quote: "I'm telling you, if I could have five lines in my .sig, I would!" -- me

==== Want to unsubscribe from Fun With Perl?
==== Well, if you insist... Send mail with body "unsubscribe" to
==== fwp-request@technofile.org