# Convert perl code to ruby: help please

**URL:** <https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803>\
**Category:** Ruby\
**Created:** [December 25, 2005, 4:14pm UTC](https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803 "2005-12-25T16:14:06Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 25, 2005, 4:14pm UTC](https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803/1 "2005-12-25T16:14:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 25, 2005, 4:14pm UTC](https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803/2 "2005-12-25T16:14:14Z")

</div>

[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

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 25, 2005, 4:14pm UTC](https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803/3 "2005-12-25T16:14:18Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![unknown](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/u/4bbf92/32.png) [@unknown](https://www.ruby-forum.com/u/unknown)\
**Post date:** [December 25, 2005, 4:15pm UTC](https://www.ruby-forum.com/t/convert-perl-code-to-ruby-help-please/75803/4 "2005-12-25T16:15:32Z")

</div>

Thank you so much! It works.  
Regards,
