Perl 6多个全局正则表达式或字符替换

时间:2017-10-11 18:11:00

标签: perl6

在Perl6中编写多个字符替换的最佳方式是什么?例如。我想用另一个字母的字母替换一个字母的字母。假设我的第一个"字母表"是abcd和我的第二个 - efgh,所以我想换取a→e,b→f,c→g,d→h。使用$pb_key = "FLWPUBK-7adb6177bd71dd43c2efa3f1229e3b7f-X"; $amount_in_naira = 900; $customer_email = "user@example.com"; $customer_firstname = "user"; $customer_lastname = "example"; $txref = "MV-1838383-JH"; $pmethod = "both"; $options = array( "PBFPubKey" => $pb_key, "amount" => $amount_in_naira, "customer_email" => $customer_email, "customer_firstname" => $customer_firstname, "txref" => $txref, "payment_method" => $pmethod, "customer_lastname" => $customer_lastname ); ksort($options); var_dump($options); $hashedPayload = ''; foreach($options as $key => $value){ $hashedPayload .= $value; } $hash = hash('sha256', $hashedPayload); echo "$hashedPayload\n"; echo "$hash"; 我会写:

sed

在Perl6中,据我所知,我应该写这样的东西:

$ echo dcba | sed 'y/abcd/efgh/'
hgfe

如果我使用20-30-40(甚至更多)字符的字母表,这将是不方便的。有没有更好的方法来解决Perl6的这个问题?

1 个答案:

答案 0 :(得分:2)

在Perl6中,有一个音译运算符tr和Perl 5一样。在Perl6中,还有一个名为trans的运算符的方法形式:

echo dcba | perl6 -pe '$_.=trans(["a".."d"] => ["e".."h"])'

<强>输出

hgfe