convert perl code to ruby: help please

V

vnpenguin

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)}
 
A

angus

[[email protected], 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
 
A

angus

[[email protected], 2005-12-25 11.02 CET]
(Perl)
$str =3D~ 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 noti=
ce
the spaces.

=E2=86=92 /([aA] (?:[(^]?['`?~.]|[(^]) )/x

Good luck.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top