* [EMAIL PROTECTED] <[EMAIL PROTECTED]> [2006-02-22 20:15]:
>map {$s+=(/\d/?$_:(ord()-55))*(36**$i++)} reverse(split(//,uc shift));
@v{ 0..9, "A".."Z" } = 0..35; $s = $s * 36 + $v{ uc $_ } for /./g;
Even readably spaced, the less clever solution is shorter.
>$s =~ s/(...)/$1-/g; print "$s\n";
print join '-', $s =~ /(..?.?)/g;
print join '-', unpack '(A3)*', $s;
Again, less clever and readably spaced is still shorter, though
the win is a lot more marginal.
With List::Util you can even write this as a single expression
(modulo initializing the lookup table, of course).
print
join '-',
unpack '(A3)*',
reduce { $a * 36 + $v{ uc $b } }
0, /./g;
I’d argue that this is rather readable too.
All solutions in direct comparison:
# 1 2 3 4 5 6 7 8
9 10
#2345689 12345689 12345689 12345689 12345689 12345689 12345689 12345689
12345689 12345689 12345689
map{$s+=(/\d/?$_:(ord()-55))*(36**$i++)}reverse(split(//,uc
shift));$s=~s/(...)/$1-/g;print"$s\n";
@v{0..9,"A".."Z"}=0..35;$s=$s*36+$v{uc$_}for/./g;print join'-',$s=~/(..?.?)/g;
@v{0..9,"A".."Z"}=0..35;$s=$s*36+$v{uc$_}for/./g;print join'-',unpack'(A3)*',$s:
@v{0..9,"A".."Z"}=0..35;print
join'-',(reduce{$a*36+$v{uc$b}}0,/./g)=~/(..?.?)/g;
@v{0..9,"A".."Z"}=0..35;print
join'-',unpack'(A3)*',reduce{$a*36+$v{uc$b}}0,/./g;
You may or may not consider it cheating that I left out the
`use List::Util 'reduce';` stanza in my `reduce` examples.
But you appear to be wanting to use this is production code, so
I don’t think pure golfage is really what you’re after.
Regards,
--
#Aristotle
*AUTOLOAD=*_;sub _{s/(.*)::(.*)/print$2,(",$\/"," ")[defined wantarray]/e;$1};
&Just->another->Perl->hacker;