in reply to numerology experiment
for, map, grep and split provide a little synergy here:
use strict; use warnings; my $str = "aabceezazerrfde"; my %wanted = (a => 7, e => 2, z => 26); my $tot = 0; my $counter = 0; $tot += $_ for map {++$counter; $wanted{$_}} grep {exists $wanted{$_}} + split //, $str; print "Found $counter letters totaling $tot\n";
Prints:
Found 9 letters totaling 81
Update: s/,/;/ in map - bug fix, thanks toolic.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: numerology experiment
by toolic (Bishop) on Nov 26, 2007 at 21:24 UTC | |
by GrandFather (Saint) on Nov 26, 2007 at 21:41 UTC |