Forum: Ruby convert perl code to ruby: help please

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 unknown (Guest)
on 2005-12-25 16:14
(Received via mailing list)
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)}
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 angus (Guest)
on 2005-12-25 16:14
(Received via mailing list)
[vnpenguin@gmail.com, 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
C2cd72c24873556e278b44b5b3c7ef33?d=identicon&s=25 angus (Guest)
on 2005-12-25 16:14
(Received via mailing list)
[vnpenguin@gmail.com, 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.
38944d5653a9e373103aaa7ff8562d8a?d=identicon&s=25 unknown (Guest)
on 2005-12-25 16:15
(Received via mailing list)
Thank you so much! It works.
Regards,
This topic is locked and can not be replied to.