Jeff, et al --

...and then Jeff 'japhy' Pinyan said...
% 
% On Aug 17, David T-G said:
% 
% >  my $string = "Hello, #NAME_FIRST# #NAME_LAST# from #STATE#!" ;
...
% >  my %xlate =
...
% >instance, in php, I would write something like
% >
% >  $fields = array('#NAME_FIRST#','#NAME_LAST#','#STATE#') ;
% >  $values = array('David','T-G','confusion') ;
% >  $custom = preg_replace($fields,$values,$string) ;
% >
% >and be done.
% 
% That's good of you to show how you'd do it in another language (yes, even
% if that language is PHP ;) ), because it clears up what you want to do.

Indeed.  It was the most succinct way to explain it :-)


% 
% There are two ways I see of doing it:
% 
%   $string =~ s{#(.*?)#}{
%     if (exists $xlate{$1}) { $xlate{$1} }  # if it's a valid key
%     else { "#$1" }  # if it's not in the hash, don't touch it
%   }ge;

This looks interesting but I don't know that I follow it all the way.

The

  s{#(.*?)#}

appears to look for my template strings.  And so then the {}ge part looks
similar to a standard s///ge, so that's probably where it's doing the
real work.

I can somewhat follow the work (thanks for your patience, if you're
reading along with me).  Based on a match like #NAME_FIRST# found in the
first line, we're going to look up the value pointed to by that hash key
in the xlate hash I built.  I have to figure that

  { $xlate{$1} }

means to return that to the caller, even though it isn't return()ed or
print()ed or whatever.

So then we get to the really confusing part...   AIUI we matched

  #key#

but here we either say that we should spit

  #key

back out or the #$1 means something that I don't get.


% 
% The other way builds a regex of the keys in the hash first:
% 
%   my $keys = join '|', map quotemeta, keys %xlate;
%   # quotemeta() make's sure any regex-characters in the keys are escaped
% 
%   $string =~ s/#($keys)#/$xlate{$1}/g;

Now this looks cool and is shorter, without necessarily being obfuscated.
That's excellent.

It's that funky map thing, though.  I don't have much experience with
that :-)

Oh, I think I get it...  We're only pulling the keys out of %xlate, and
we're quoting them to be safe.  And you picked '|' for the join because
that's a regexp or, right?  So my expr ends up being like

  s/#(NAME_FIRST|NAME_LAST...)#/...

and the replacement is the bucket value of the key in the hash.  Hey,
that's slick.

So why the map?  Is that just a way to apply quotemeta to each key in the
hash?


% 
% You'll notice for the second code we didn't need the 'e' modifier.  That's
% because in the first chunk of code, the replacement was a function -- that
% is, it wasn't just a string, it was code that needed to be executed.  The

Right.


% second time, though, we know $1 is a valid key in the hash, so there are
% no precautions needed.

Got that part, too.


Thanks again & HAND

:-D
-- 
David T-G                      * There is too much animal courage in 
(play) [EMAIL PROTECTED] * society and not sufficient moral courage.
(work) [EMAIL PROTECTED]  -- Mary Baker Eddy, "Science and Health"
http://justpickone.org/davidtg/      Shpx gur Pbzzhavpngvbaf Qrprapl Npg!

Attachment: pgp00000.pgp
Description: PGP signature

Reply via email to