use strict ; use warnings ; my $input = "NDDDDTSVCLGTRQCSWFAGCTNRTWNSSA 0" ; my @F = split'',$input ; my @ar ; @ar[map { ord } split '','ARNDBCEQZGHILKMFPSTWYV'] = 1..127 ; foreach ( @F ) { print( ($ar[ord $_] // $_ ) . " " ) ; }
But I can't figure out how to put this in a one-liner, does anyone want to try?
edit Feb 22, 2019 at 10:20 UTC:
Up till now I have:
perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '($_=$F[0])=~s/(.)/($ar[ord $1])." "/ge' input.txt # or perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '($_=$F[0])=~s/(.)/(defined $ar[ord $1] ? $ar[ord $1] : $1 ) +." "/ge' input.txt
As rsFalse indicated in his reply below there is a trailing space. The other thing is that everything after a space is gone missing and I can't figure out why.
edit Feb 22, 2019 at 10:49 UTC:
Thanks Eily and Discipulus for pointing out -MO=Deparse to me. The mistake that I made was: $_=$F[0] So this should work now:
# 'defined' is optional: perl '-M5;@ar[map { ord } split "","\0ARNDBCEQZGHILKMFPSTWYV"] = 0..12 +7' -alpe '$_=~s/(.)/(defined $ar[ord $1] ? $ar[ord $1]." " : $1 )/ge' + input.txt
One more edit: Also note that I specifically crafted this in this manner so that this would also work:
perl '-M5;@ar[map { ord } split "","\0V\0B\0L\0\0\0Q\0\0\0\0\0\0\0E\0\ +0\0\0\0\0\0\0\0\0P\0\0I\0\0\0\0\0\0Z\0H\0\0\0\0\0\0\0\0Y\0J\0\0S\0\0\ +0\0\0D\0\0\0X\0\0\0\0\0K\0\0\0R\0\0\0\0W\0\0\0\0\0U\0\0\0\0A\0\0M\0\0 +\0F\0\0\0C\0\0T\0\0\0\0O\0\0\0\0\0\0\0\0\0\0N\0G\0\0\0\0"] = 0..127' +-alpe '$_=~s/(.)/($ar[ord $1] ? $ar[ord $1]." " : $1 )/ge' input.txt
In reply to Re: A little golfing challenge: Replacing letters with numbers (edit)
by Veltro
in thread A little golfing challenge: Replacing letters with numbers
by haukex
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |