Jeff, et al -- ...and then Jeff 'japhy' Pinyan said... % % On Aug 17, David T-G said: % % >% 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 :-) % % This list (and many others) often receives questions about code without
Heh. I know; I used to hang out here much more :-)
% ever showing even pseudo-code. PHP has similar-enough syntax to Perl, so
% it's fine.
Indeed. In fact, it was a breeze to pick up because I kn[oe]w perl.
%
% >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.
%
% Yes. I'm using s{}{} instead of s///. I prefer the use of {} delimiters
% when I'm doing a s///e, because it better represents the "code" aspect of
% the replacement.
Ohhhhhhh... Slick. Can I use () or perhaps even [] as well?
%
...
% > { $xlate{$1} }
% >
% >means to return that to the caller, even though it isn't return()ed or
% >print()ed or whatever.
%
% Yes; perhaps I should have been more explicit and said 'return ...'.
OK; as long as I'm still hanging on it's fine :-)
%
...
% > #key
% >
% >back out or the #$1 means something that I don't get.
%
% Sorry, that was my typo. It should be
*whew*
%
% else { return "#$1#" }
%
% That is, if the text found between #'s isn't a key in the hash, just
% return the text with the #'s on either side of it.
Good deal. So I followed the whole thing. Awesome! :-)
%
...
% >It's that funky map thing, though. I don't have much experience with
% >that :-)
%
% A simple way to look at map() and grep() is that they are merely for-loops
% that automatically build up a return list for you. Here's a brief look at
% their equivalence:
[snip]
Thanks for the clear explanation. That's a lot of help.
Hey, map is slick :-)
%
% Compounded to this is the fact that both functions can be called with an
% EXPRESSION, rather than a BLOCK:
%
% @halves = map $_/2, @numbers;
% @halves = map { $_/2 } @numbers;
%
% mean the same thing really. I used the 'expression' syntax.
Yep. Very perl-ish and not surprising.
%
...
% > s/#(NAME_FIRST|NAME_LAST...)#/...
% >
% >and the replacement is the bucket value of the key in the hash. Hey,
% >that's slick.
%
% Exactly.
Neat.
%
% >So why the map? Is that just a way to apply quotemeta to each key in the
% >hash?
%
% Yes. If we wrote the map() as a for loop:
%
% my @safe;
% for (keys %xlate) {
% push @safe, quotemeta; # it's default arg is $_
% }
% my $pattern = join '|', @safe;
So I see. Very cool.
And my code runs! It now looks like
...
42 ### loop thru list
43 while ( my @row = $result->fetchrow_array )
44 {
45 my ($ascii, $html, $flag, $email, $fn, $ln) = @row ;
46 my $body =
47 parseit
48 (
49 {ASCII=>$ascii,HTML=>$html},
50 {flag=>$flag,EMAIL=>$email,NAME_FIRST=>$fn,NAME_LAST=>$ln}
51 ) ;
52 print "I got '$body' for .$row[3].\n";
53 }
...
62 # parse function
63 sub parseit
64 {
65 my ($t,$u) = @_ ;
66 my %template = %$t ;
67 my %userdata = %$u ;
68 my $body =
69 $userdata{flag} ?
70 $template{HTML} : $template{ASCII} ;
71 my $keys = join('|',map(quotemeta,keys(%userdata))) ;
72 $body =~ s/#($keys)#/$userdata{$1}/g ;
73 return $body ;
74 }
...
and works dandily :-) I could probably clean up the two hashes I send to
parseit. Hey, I wonder if
my ($template{ASCII},$template{HTML},
$userdata{flag},$userdata{EMAIL},$userdata{NAME_FIRST},
$userdata{NAME_LAST}) = @row
because each of those is a single value...
/me wanders off muttering, but gleefully
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!
pgp00000.pgp
Description: PGP signature
