Can I use the y operator to transliterate more than one to one in Perl?

The y operator in Perl translates one character at a time. For example, if we execute y / abc / dfg on the string "foobar", we get "foofdr". But what if I want to transliterate "ā" to "ei" and "ä" to "a" and "ō" to "әu" and "o" to "ɒ".

I tried the following line of code but no luck :(

y/āäōo/(ei)(a:)(әu)ɒ/

Do we have a workaround to solve this problem? Or do I need to reuse the s operator and make many cumbersome replacements?

Thanks in advance for any recommendations :)

+3
source share
2 answers

.

use warnings;
use strict;
use utf8;
binmode STDOUT, ":utf8";
my $string = "āäōo";
my %trans = qw/ā ei ä a: ō u o ɒ/;
my $keys = join '', keys %trans;
$string =~ s/([$keys])/$trans{$1}/g;
print "$string\n";

, , , ( | | ) [ ].

+14

Source: https://habr.com/ru/post/1724581/


All Articles