質問

I would like to replace a string with the numerical correspondent.

For example (one-liner on Windows):

perl -e "$_ = \"abcdefghijklmnopqrstuvwxyz\"; tr\a-z\1-9\;"

The result is:

12345678999999999999999999  

This works until 9 but how I can assign the numeric correspondent after character i?
I would like to know how I can assign 2 sign to one 1 sign,

for example,

12 -> j, 13 -> k, etc.

To identify the numerical value it would makes sense to assign

"1-", "2-", ... "25-", "26". 
役に立ちましたか?

解決

perl -E"$_ = 'abcdefghijklmnopqrstuvwxyz'; s/([a-z])/ord($1)-96/ge; say;"

or if you have 5.14+

perl -E"say 'abcdefghijklmnopqrstuvwxyz' =~ s/([a-z])/ord($1)-96/ger;"

You can substitute any rule instead of ord($1) - 96.

他のヒント

I don't believe tr/// can do that unfortunately - it's a one-to-one character substitution. So you're going to have to go the long way round:

my %indicies = map { $_ => (ord($_) - ord('a')) + 1 } ('a' .. 'z');
my $result = join '', map { $indicies{$_} } split(//, $string);

Unfortunately that's not a one-liner.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top