#!/usr/bin/env perl
use warnings;
use strict;
my $input = "test";
my @arr = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
my @cip = ('%', '@', '!', '^', '*', '_', '+', '&', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', 'A', 'B', 'D', '$', '(', ')', '/', '|');
<do something here>
print $res;
输出应根据数组:B*AB
唯一的问题是我不知道如何到达那里,我尝试使用正则表达式中的 s//但它似乎不起作用。
最佳答案
您可以使用哈希查找表将字符替换为字符,其中 @arr
元素是键,@cip
是值。 "\U$1"
对捕获的字符串进行大写。
use warnings;
use strict;
my $input = "test";
my @arr = ('A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');
my @cip = ('%', '@', '!', '^', '*', '_', '+', '&', '2', '3', '4', '5', '6', '7', '8', '9', '0', '1', 'A', 'B', 'D', '$', '(', ')', '/', '|');
my %h;
@h{@arr} = @cip;
(my $res = $input) =~ s/(.)/ $h{"\U$1"} /ge;
print $res;
输出
B*AB
关于arrays - 替换数组中的字母,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24331711/