It's kinda hard to see but I included the /x switch because I inserted blanks on the pattern as well as the replacement side. Without /x, the match will fail.
$str =~ s{ ([^[:print:]]) }{ sprintf( "(%#2X)", ord $1) }gex; ^ ^ On Tue, Sep 6, 2016 at 1:06 PM, Uri Guttman <u...@stemsystems.com> wrote: > On 09/06/2016 03:59 PM, X Dungeness wrote: >> >> $str = "ab\rcd\nef\ngh\fij"; >> >> $str =~ s{ ([^[:print:]]) }{ sprintf( "(%#2X)", ord $1) }gex; >> >> ----> ab(0XD)cd(0XA)ef(0XA)gh(0XC)ij >> >> > that is a nice use of /e (don't think you need /x when you already have /e > as code can handle blanks. but the # comment feature is enabled with /x.). > but the other answers had a nice optmization. they built the hash of numbers > to hex strings so their replacement only did a hash lookup and they don't > even need /e to work. that is a basic caching optimization that newbies can > learn to use. this is a good case to see the coding differences. as an > exercise, some of you could even write up a benchmark comparing them. post > your results to this thread. > > thanx, > > uri > > > > -- > To unsubscribe, e-mail: beginners-unsubscr...@perl.org > For additional commands, e-mail: beginners-h...@perl.org > http://learn.perl.org/ > > -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/