in reply to Converting pascal code to perl

You know, I find the Pascal code far clearer than your Perl code. One glance at the Pascal code, and it's immediate clear to me what it does, it returns the chance of rolling the given number with two dice.

I don't get that from the Perl code - the symmetry of the case statement is lost in the hash.

Abigail

Replies are listed 'Best First'.
Re: Re: Converting pascal code to perl
by mp (Deacon) on Aug 15, 2002 at 16:39 UTC
    This version is not any shorter than the Pascal version, but perhaps nearly as clear to someone that understands perl regexes.
    use strict; use warnings; sub roll { my $point = int($_[0]); # Just to be safe $point =~ /^(?:2|12)$/ && return 1/36; $point =~ /^(?:3|11)$/ && return 2/36; $point =~ /^(?:4|10)$/ && return 3/36; $point =~ /^(?:5|9)$/ && return 4/36; $point =~ /^(?:6|8)$/ && return 5/36; $point == 7 && return 6/36; warn "Invalid roll"; return 0; }