用数组代替字母

时间:2014-06-20 16:23:44

标签: arrays regex perl encryption

#!/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 //,但这似乎没有用。

6 个答案:

答案 0 :(得分:3)

您可以使用哈希查找表替换char,其中@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

答案 1 :(得分:2)

你可以这样做:

#!/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', '$', '(', ')', '/', '|');

# Loop through each index in the first array
for my $index (0..$#arr) {
    # search for the element at that index in the first array
    my $search = $arr[$index];

    # Replace it with the corresponding element in the next array
    my $replace =$cip[$index];

    # Search and replace, /i for ignore case and /g for replacing all instances
    $input =~ s/$search/$replace/gi;
}
my $res = $input;
print $res;

输出:

Matt@MattPC ~/perl/testing/9
$ perl test.pl
B*AB

答案 2 :(得分:2)

#!/usr/bin/perl

use strict;
use warnings;
use 5.010;

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 %translate;

@translate{@arr} = @cip;

$input =~ s/(\w)/$translate{uc $1}/eg;

say $input;

答案 3 :(得分:2)

使用哈希:

use strict;
use warnings;

my $input = "test";

my @keys = qw{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 @vals = qw{% @ ! ^ * _ + & 2 3 4 5 6 7 8 9 0 1 A B D $ ( ) / |};

my %hash;
@hash{@keys} = @vals;

my $output = join '', map {$hash{uc $_} // warn "not found $_"} split '', $input;

print $output;

输出:

B*AB

答案 4 :(得分:1)

只是为了衡量,使用音译操作符(tr//)的答案。

my @arr = qw(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 = qw(% @ ! ^ * _ + & 2 3 4 5 6 7 8 9 0 1 A B D $ ( ) / |);
my $arr = join "", @arr;
my $cip = join "", @cip;

my $res;
eval "(\$res = uc \$input) =~ tr/\Q$arr\E/\Q$cip\E/";
print "test => $res\n"; # B*AB

不幸的是,在tr//中使用变量需要使用eval。如果列表总是相同,则可以执行类似tr/A-Z/\%\@.../的操作,并避免使用eval。

答案 5 :(得分:0)

一个稍微不同的音译示例,即分隔符安全:

#!/usr/bin/perl

use warnings;
use strict;

my $input = "test";
my @arr   = qw(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   = qw(% @ ! ^ * _ + & 2 3 4 5 6 7 8 9 0 1 A B D $ ( ) / |);

my $plaintext  = join '', @arr;
my $ciphertext = join '', @cip;

$_ = uc $input;
eval(sprintf("tr/%s/%s/", map { quotemeta($_) . "" } ($plaintext, $ciphertext)));
my $res = $_;

print "$res\n";