Tomorrow at 2:37am, YANAGAWA Kazuhisa said:

> Well.... something like following?
>
>     > ruby <<EOS
>     str = "This is a &lt;string&gt; and <string2>."
>     map = {"&lt;" => "<", "&gt;" => ">", "<" => "&lt;", ">" => "&gt;"}
>     p str.gsub(/#{map.keys.join("|")}/) { map[$&] }
>     EOS
>     "This is a <string> and &lt;string2&gt;."
>
> # ....oops I slightly misread your posting.... uh, a problem is not so
> # large, isn't it? :-P

Well, it's almost what I wanted in my real app ;-) I want:

"\>"  becomes "&gt;"
"\<"  becomes "&lt;"
"<<"  becomes "<strong>"
">>"  becomes "</strong>"
"<"   becomes "<em>"
">"   becomes "</em>"

something like that.  The problem is that the order of the keys does
matter.  So I have to make the keys an array in itself, in a fixed order
and indeed use a hash to fetch the wanted replacement value.

(update 12-06-2001:) Another question: even when using a ruby array for
the map (because of the order), will the regexp match the joined keys in
the same order? Won't the '<' be found before the '<<'? (Tcl 8.1+ regexp
engine, with '|' in between do match in an undefined order.)

This works in ruby:

map_re = /\\>|\\<|<<|>>|<|>/
map = {
 "\\>" => "&gt;",
 "\\<" => "&lt;",
 "<<"  => "<strong>",
 ">>"  => "</strong>",
 "<"   => "<em>",
 ">"   => "</em>"
}

str = "This --\\> is a <emphasized string>, <<this>> bold."
# (the double backslash becomes one in the string.)

p str.gsub(map_re) {map[$&]}

But will it always be guaranteed that the keys in the regexp are matched
on a base of 'first one found matches'?

Btw, I would really opt for a 'string map' like feature. Tcl's is very
fast, it works w/o regexps.

Thanks all for sharing your comments,

regards,
Wilbert

-- 
 /"\  . . . . . . . . . . . . . . . . . . . . . . . . . . . . .  /"\
 \ /   ASCII Ribbon Campaign       Wilbert Berendsen             \ /
  X   - NO HTML/RTF in e-mail      http://www.xs4all.nl/~wbsoft   X
 / \  - NO MSWord docs in e-mail   http://linuxathome.nl         / \