Convert perl code to ruby: help please


#1

Hi all,
I have a following Perl code:

%charmap =(
“a" => "\xc3\xa0", "a?" => "\xe1\xba\xa3", "a~" => "\xc3\xa3", "a'" => "\xc3\xa1", "a." => "\xe1\xba\xa1", "a(" => "\xc4\x83", "a(” => “\xe1\xba\xb1”,
“a(?” => “\xe1\xba\xb3”,
“a(~” => “\xe1\xba\xb5”,
“a(’” => “\xe1\xba\xaf”,
“a(.” => “\xe1\xba\xb7”,
“a^” => “\xc3\xa2”,
“a^`” => “\xe1\xba\xa7”,
“a^?” => “\xe1\xba\xa9”,
“a^~” => “\xe1\xba\xab”,
“a^’” => “\xe1\xba\xa5”,
“a^.” => “\xe1\xba\xad”
)

$str =~ s{ ( [aA] (?: [(^]? [’`?~.] | [(^] ) )}{$charmap{$1}}egox;

Just for converting from VIQR (a 7bit charset for Vietnamese) into
UTF-8.

I’m trying to do the same method in Ruby:

charmap = {
“a" => "\xc3\xa0", "a?" => "\xe1\xba\xa3", "a~" => "\xc3\xa3", "a'" => "\xc3\xa1", "a." => "\xe1\xba\xa1", "a(" => "\xc4\x83", "a(” => “\xe1\xba\xb1”,
“a(?” => “\xe1\xba\xb3”,
“a(~” => “\xe1\xba\xb5”,
“a(’” => “\xe1\xba\xaf”,
“a(.” => “\xe1\xba\xb7”,
“a^” => “\xc3\xa2”,
“a^`” => “\xe1\xba\xa7”,
“a^?” => “\xe1\xba\xa9”,
“a^~” => “\xe1\xba\xab”,
“a^’” => “\xe1\xba\xa5”,
“a^.” => “\xe1\xba\xad”}

def vconv (str)
str.gsub(/([aA] (?:[(^]?[’`?~.]|[(^]) )/){ |k| charmap[k] }
end
$stdin.each {|line| puts vconv(line)}


#2

[removed_email_address@domain.invalid, 2005-12-25 11.02 CET]

I’m trying to do the same method in Ruby:

charmap = {

“a^.” => “\xe1\xba\xad”}

def vconv (str)
str.gsub(/([aA] (?:[(^]?[’`?~.]|[(^]) )/){ |k| charmap[k] }
end
$stdin.each {|line| puts vconv(line)}

Hi,

from vconv you cannot see charmap (different scopes). You can make
charmap a
constant (change the name to CHARMAP) or a global variable ($charmap).

HTH


#3

[removed_email_address@domain.invalid, 2005-12-25 11.02 CET]
(Perl)

$str =~ s{ ( [aA] (?: [(^]? [’`?~.] | [(^] ) )}{$charmap{$1}}egox;

(Ruby)

str.gsub(/([aA] (?:[(^]?[’`?~.]|[(^]) )/){ |k| charmap[k] }

You also should put a ‘x’ in the regexp options ^, at first I didn’t
notice
the spaces.

â?? /([aA] (?:[(^]?[’`?~.]|[(^]) )/x

Good luck.


#4

Thank you so much! It works.
Regards,