Perl 翻译

发布于 2024-11-15 19:20:01 字数 232 浏览 4 评论 0原文

我想做这样的事情

$string ='4791';
$string =~ tr/4791/(ma)(laya)(lam)(_baasha)/;

应该给我

$string='malayalam_baasha';

即用其他 n 个字符替换每个字符。每个字符的 n 可能不同。

这个翻译有单行解决方案吗?

I want to do something like this

$string ='4791';
$string =~ tr/4791/(ma)(laya)(lam)(_baasha)/;

should give me

$string='malayalam_baasha';

i.e replace each character with n other characters. n may be different for each character.

Is there a one line solution for this translation ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

时光清浅 2024-11-22 19:20:01

假设您总是想用特定字符串替换单个字符...

my %Replacement = (
    '0' => 'abc',
    '1' => 'def',
    '2' => 'ghi',
    '3' => 'jkl',
    # ... whatever others you like ...
);

my $String = '0123';
print "$String\n"; # Prints "0123"
$String =~ s{(.)}
            {exists($Replacement{$1}) ? $Replacement{$1} : $1}egx;
print "$String\n"; # Prints "abcdefghijkl"

只需在 %Replacement 中为您想要换出的每个字符输入一个条目。

重新阅读你的问题,不,这不是一行,尽管如果你愿意的话可以这样写(尽管很混乱)。不过,将其限制在一行实际上取决于您想要有多少个不同的交换。过了某个点,它就会变得丑陋。

Assuming you always want to replace a single character with a specific string...

my %Replacement = (
    '0' => 'abc',
    '1' => 'def',
    '2' => 'ghi',
    '3' => 'jkl',
    # ... whatever others you like ...
);

my $String = '0123';
print "$String\n"; # Prints "0123"
$String =~ s{(.)}
            {exists($Replacement{$1}) ? $Replacement{$1} : $1}egx;
print "$String\n"; # Prints "abcdefghijkl"

Just make an entry in %Replacement for each character you want to swap out.

Re-reading your question, no, this isn't on one line, though it can be written as such (though messily) if you like. Constraining it to a single line will really kind of depend on how many different exchanges you want to have, though. After a certain point, it's going to get ugly.

抱着落日 2024-11-22 19:20:01

正确的答案是 Brian Gerard 的,但它可以用一行相当短且几乎可读的行来完成:

$string =~ s/(.)/{1 => "_baasha", 4 => "ma", 7 => "laya", 9 => "lam"}->{$1}/ge;

或一行短的不可读行:

$string =~ s/(.)/{4,ma=>7,laya=>9,lam=>1,"_baasha"}->{$1}/ge;

甚至更短,但更具可读性:

$string =~ s/(.)/qw(a _baasha a a ma a a laya a lam)[$1]/ge;

或我能得到的最短的答案(这个不会在 strict 打开的情况下工作):

$string =~ s/(.)/(a,_baasha,a,a,ma,a,a,laya,a,lam)[$1]/ge;

The right answer is Brian Gerard's, but it can be done in one fairly short and almost readable line:

$string =~ s/(.)/{1 => "_baasha", 4 => "ma", 7 => "laya", 9 => "lam"}->{$1}/ge;

or one short unreadable line:

$string =~ s/(.)/{4,ma=>7,laya=>9,lam=>1,"_baasha"}->{$1}/ge;

or even shorter, but a bit more readable:

$string =~ s/(.)/qw(a _baasha a a ma a a laya a lam)[$1]/ge;

or the shortest I could get it (this one won't work with strict turned on):

$string =~ s/(.)/(a,_baasha,a,a,ma,a,a,laya,a,lam)[$1]/ge;
千里故人稀 2024-11-22 19:20:01

这个

($i=0) || (@tr = qw |abc def ghi jkl| ) && (@string = map { $tr[$i++] } split //,'0123') && ($string =join '',@string);

OR

( %tr = ( 0 => 'abc' , 1 => 'def' , 2 => 'ghi' , 3 => 'jkl' ) ) && (@string = map { $tr{$_} } split //,'0123') && ($string =join '',@string); 

应该有效!但我不会用它!

This

($i=0) || (@tr = qw |abc def ghi jkl| ) && (@string = map { $tr[$i++] } split //,'0123') && ($string =join '',@string);

OR

( %tr = ( 0 => 'abc' , 1 => 'def' , 2 => 'ghi' , 3 => 'jkl' ) ) && (@string = map { $tr{$_} } split //,'0123') && ($string =join '',@string); 

should work! But I wouldn't use it!!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文