in reply to numerology experiment
I'm late on this but here's a version I liked, for brevity:
use strict; use warnings; my $str = "aabceezazerrfde"; my %wanted = (a => 7, e => 2, z => 26); my $tot = 0; $wanted{$_} && ($tot += $wanted{$_}) for (split //, $str); print "Total $tot\n";
Better in some circumstances might be:
exists $wanted{$_} && ($tot += $wanted{$_}) for (split //, $str);
but I don't think it matters in this case. I discussed mixing "for" and "if" in one line with moritz and shmem recently, and I rather like it, though someone said it was too much logic for one line...
($tot += $wanted{$_}) if $wanted{$_} for (split //, $str);
never seems to work...
Cheers
|
---|