Warning: useless use of map in void context.
Ok, so perl may not be giving that to you, but some of us think it should ;-)
This is the same, but has no return value (well, other than the final $sum, but most of us forget that):
$sum += $index{$_} for split //, $word;
Though, that said, I would still end up using map:
use List::Util qw(sum)
my $sum = sum map $index{$_}, split //, $word;
My method has the advantage of reading more like English, and the disadvantage of likely being slower and using more memory. That said, it's always important to me that my code looks more like my design docs than like the underlying machine code, though I have to admit that $sum += $index{$_} is just so idiomatic that my mind sees "sum of $index{$_}" when my eyes see "$sum += $index{$_}". I just had to present it as yet another WTDI. |