在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的这个问题?
答案 0 :(得分:2)
在Perl6中,有一个音译运算符tr
和Perl 5一样。在Perl6中,还有一个名为trans
的运算符的方法形式:
echo dcba | perl6 -pe '$_.=trans(["a".."d"] => ["e".."h"])'
<强>输出强>:
hgfe