Write a function that accepts string as a parameter, returning evaluated value of expression in dice notation, including addition and multiplication.
To
perl eval version, 72 chars
sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 0...$1-1;eval$_}
runs like
print e("4*d12+3"),"\n";
Based on the ruby solution, can only run once(you should undef $a
between runs).
shorter version, 68 chars, funky (0 based) dice
sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=int rand$2for 0...$1-1;eval$_}
EDIT
perl 5.8.8 didn't like the previous version, here's a 73 char version that works
sub e{$_=pop;s/(\d+)?d(\d+)/\$a/i;$a+=1+int rand$2-1for 1...$1||1;eval$_}
70 char version supporting multiple rolls
sub e{$_=pop;s/(\d*)d(\d+)/$a=$1||1;$a+=int rand$a*$2-($a-1)/ieg;eval}