laziness, impatience, and hubris | |
PerlMonks |
A little golfing challenge: Replacing letters with numbersby haukex (Archbishop) |
on Feb 21, 2019 at 08:48 UTC ( [id://1230284]=perlquestion: print w/replies, xml ) | Need Help?? |
haukex has asked for the wisdom of the Perl Monks concerning the following question: In this StackOverflow question, user "Learner" asked how to replace a set of certain letters in the first column of a file with numbers. In other words, given this set of replacements:
And this input:
The expected output is:
Here are my two solutions:
I personally consider the extra space character at the end of the line produced by my solutions acceptable (I think diff -b is probably ok too). Unfortunately the OP didn't specify what would happen in case the input strings contained letters that aren't in the set, so I guess "bonus points" for solutions that only affect [A-IK-NP-TV-WY-Z] instead of [A-Z] like my solution does. Bonus question: Can anyone come up with a short, preferably pure Perl, solution to produce such a regex character set for any given list of letters?
Have at it ;-) Update: Thank you for the inspired and inspiring responses so far, Discipulus, Eily, rsFalse, Veltro, and vr! I really enjoy the creativity in the solutions :-)
Back to
Seekers of Perl Wisdom
|
|