Perl 翻译
我想做这样的事情
$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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
假设您总是想用特定字符串替换单个字符...
只需在 %Replacement 中为您想要换出的每个字符输入一个条目。
重新阅读你的问题,不,这不是一行,尽管如果你愿意的话可以这样写(尽管很混乱)。不过,将其限制在一行实际上取决于您想要有多少个不同的交换。过了某个点,它就会变得丑陋。
Assuming you always want to replace a single character with a specific string...
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.
正确的答案是 Brian Gerard 的,但它可以用一行相当短且几乎可读的行来完成:
或一行短的不可读行:
甚至更短,但更具可读性:
或我能得到的最短的答案(这个不会在
strict
打开的情况下工作):The right answer is Brian Gerard's, but it can be done in one fairly short and almost readable line:
or one short unreadable line:
or even shorter, but a bit more readable:
or the shortest I could get it (this one won't work with
strict
turned on):这个
OR
应该有效!但我不会用它!
This
OR
should work! But I wouldn't use it!!