I stumbled across Dice::Simple and RPG::Dice in the code section, and it reminded me of a Pascal program I wrote back in the early '90s for a class. A typical calculator assignment, that I later tweaked for RPG use. I'd love to port it to Perl, but because of the syntax I can't cheat and use eval() like the aforementioned modules. Attached is the help doc as well as a few run examples. Should I change the syntax, or what's a good starting point other than brute-force character-by-character parsing as I did originally?
Help Doc: ---------------------------- Key: n = number (integers only) l = list (of anything) ln = list of numbers exp = expression (i.e. any valid expression) Operators: +,-,/,* Integer math only d ex. n1dn2 = Roll a n2 sided die n1 times and sum the results. @[] ex. n@[expr] = evaluate expr n times and put the results in a list. <,> Selectors. ex. n1>ln = select the greatest n1 items from the list ln. & ex. &ln = sum the numbers in the list ln. Default order of prececence, highest to lowest: ()'s d *,/ +,- @ >,< & Examples: 3d6 ! Returns a value 3 to 18. (1d4)d20 ! Rolls a 20 sided die 1 to 4 times and sums the results. 6@[3d6] ! Returns 6 values of 3 to 18 in a list. 6>12@[3d6] ! Returns the best 6 of 12 3d6's in a list. 6@[&3>4@[(1d5)+1]] ! Equivelant to the ol' "Roll 6 stats -- 4d6, re-roll 1's, drop the lowest die." Note that n1dn2 is logically equivelant to &n1@[1dn2] ---------------------------- Sample output: ---------------------------- Command: 3d6 Value: 9 Command: 3d6 Value: 10 Command: 6@[3d6] ( 14 13 11 13 9 13) Command: 6@[3d6] ( 7 11 15 17 7 7) Command: 6@[&3>4@[(1d5)+1]] ( 14 12 15 12 10 11) Command: 6@[&3>4@[(1d5)+1]] ( 16 15 12 14 16 14) Command: 4@[6@[3d6]] ( ( 13 8 6 9 4 12) ( 13 7 9 10 11 9) ( 9 12 10 9 13 13) ( 10 8 9 7 7 12) ) Command: X = 3d6 Symbol "X" created. Value: 0 Command: 4@[X] ( 10 12 14 9) Command: X<20@[X] ( 6 7 7 7 8 9 9 10 10 10 11 11 11 12 12) Command: X<20@[X] ( 4 5 7 7) ----------------------------

In reply to Dice calcs? by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.