#!/usr/bin/perl # Incept: 1998-10-18 15:13:27 CDT (Oct Sun) http://www.talisman.org/~erlkonig/ # odds and score of 2d6 # 0 1 2 3 4 5 6 7 8 9 10 11 12 @odds = ( 0,0, 1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1 ); @scores = ( 0,0, 10, 10, 40, 40, 40, -10, 40, 40, 40, -10, 0 ); $possibilities = 36; sub main # main routine, called near end of script { my $score = 0; # make a variable, for main only, named score $score = $ARGV[0] if($#ARGV >= 0); # look for a startscore on the cmdline # strings of characters (text) are concatenated (joined) with the dot print("initial score is " . $score . "\n"); # "\n" is a newline for(;;) # forever, special case of for loop { print "------input roll: "; # notice no \n my $roll = ; # read one line of input from standard in # regular expressions allows text to be described. \d is a digit # + describes "one or more" of the thing to its left # hence, \d+ is "one or more" digits. =~ triggers use of regexp if($roll =~ /\d+/) # if input is a string of one or more digits { my $score_change = $scores[$roll]; # get score for specific roll print("odds of roll: " . $odds[$roll] . "/" .$possibilities ."\n"); print("result of roll: " . $score_change . "\n"); $score += $score_change; # or: $score = $score + $score_change; print("updated score: " . $score . "\n"); } else # if the "if" failed (input wasn't a number) { print("quitting\n"); last; # would be "break" in C or the shells } } return 0; # exit from nearest enclosing function (main) } &main; #--------------------------------------------eof