From: Wilbert Berendsen Date: 2001-06-08T21:47:29+09:00 Subject: [ruby-talk:16325] Tcl string map lookalike Hi, What's the best way to perfomrs a Tcl-like "string map" function. Tcl "string map" walks through a string, replacing every occurrence of a substring with an associated replacement. The substrings and replacement are in a flat list, and the list is searched from the beginning, so first matches precede, eg: Tcl: set str "This is a <string$gt;" set map [list "<" "<" "<" ">" "<" "<" ">" ">"] string map $map $str ===> "This is a " Ruby: (????) str = "This is a string" map = ["<", "<", "<", ">", "<", "<", ">", ">"] str.map (map) ===> "This is a " I tried using gsub with an array and a special regexp, but that's a bit cumbersome. Separate gsub's after eachother don't work, because the substitutions may again contain matching character which results in errors. So it must do it in one single pass along the string. Any ideas? Thanx, 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 / \