#!/usr/bin/perl -w use strict; use vars qw($answer $correct $num @arr $errs); $errs=0; # Build questions foreach my $i (1..10) { foreach my $j (1..10) { push (@arr,"$i * $j"); } } # Loop block { # Exit when all Q's are spliced away last unless defined $arr[0]; # Get a random question (This will leave # 10 times 10 as the last question asked. # Use @arr instead of $#arr if you don't # like that behaviour. Please refer to # chipmunk's post for an explanation of # why.) $num=int(rand $#arr); # Find the answer $correct=eval($arr[$num]); # Prompt for answer print "What is $arr[$num] ? \n"; # Answering block { $answer=; chomp $answer; redo unless $answer=~m/^\d+$/; } # Right or wrong ? if ($answer == $correct) { print "Good, that's correct.\n"; splice(@arr,$num,1); } else { print "No, that's not correct, $arr[$num] = $correct\n"; $errs++; } # Tell how much it's left print "".($#arr+1)." questions left.\n\n"; # And again... redo; } print "All done.\nYou made $errs misstakes.\n";