Can I use the y operator to do a non-one-to-one transliteration in Perl?

305 Views Asked by At

The y operator in Perl does character-by-character transliteration. For example, if we do y/abc/dfg to 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 hopefully have a workaround for this problem? Or do I have to repeatedly use the s operator and do a lot of cumbersome substitutions?

Thanks in advance for any guidance :)

2

There are 2 best solutions below

4
On BEST ANSWER

In this case, create a hash and go from the keys to the strings easily.

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";

You need to alter this if your keys are more than one character long by sorting the keys in order of decreasing length and joining them using ( | | ) instead of [ ].

2
On

It sounds like you're trying to do something similar to Text::Unaccent::PurePerl.