Replace letters by their rank, keeping punctuation intact
Ruby 2.0, 41+1 characters
There, now it complies with the updated rules and makes prettier output.
gsub(/\w+/){$&.chars.map{|c|c.ord&31}*?-}
Run with -p
switch:
ruby -p ord.rb <<< "ABC! Defghi, jklmnopqr stuvwxyz"
1-2-3! 4-5-6-7-8-9, 10-11-12-13-14-15-16-17-18 19-20-21-22-23-24-25-26
Perl: 42 38 characters
s/\S(?=\S)/$&-/g;s/[a-z]/31&ord$&/ige
Sample run:
bash-4.1$ perl -pe 's/\S(?=\S)/$&-/g;s/[a-z]/31&ord$&/ige' <<< "Here's another example, I hope that this will illustrate my point! If it's unable, more specs will be given."
8-5-18-5-'-19 1-14-15-20-8-5-18 5-24-1-13-16-12-5-, 9 8-15-16-5 20-8-1-20 20-8-9-19 23-9-12-12 9-12-12-21-19-20-18-1-20-5 13-25 16-15-9-14-20-! 9-6 9-20-'-19 21-14-1-2-12-5-, 13-15-18-5 19-16-5-3-19 23-9-12-12 2-5 7-9-22-5-14-.
GolfScript, 37 36 23 characters
The following version takes newline as delimiter (as Peter's solution):
{.32|96^:@`]@27,?0>=n}%
A version which interprets words and delimiters as in the examples given is 36 characters long:
' '/{'-'*{.32|96^:@`]@27,?0>=}%' '}%
(Strange enough my first version '-''- -'/' '{.32|96^:@`@0>@27<&if}%
didn't work...)
Takes input on STDIN. The example can be tested online:
> 'Hello world!'
8-5-12-12-15 23-15-18-12-4-!