#!usr/bin/perl -w use strict; my %scores; print "Current Scores for each player:\n"; print "#Tries\tName\n"; open FILE, '<' ,"high_scores.dat" or die "can't open high score file $!"; while () { chomp; my ($score, $name) = split ',',$_; print "$score\t$name\n"; $scores{$name}=$score; } close FILE; print "Starting the guessing game...I've picked a number between 1 and 100\n"; my $goal = int(rand(100)) + 1; my $guess=0; my $count=1; while ( (print "Enter your guess: "), ($guess= ) !~ m/\s*(quit|exit)\s*$/ ) { next if $guess =~ /^\s*$/; # just reprompt for blank lines # does not increment $count chomp $guess; if ($guess =~ m/\D/) { print "Only numbers are allowed!\n"; next; } if ($guess <1 or $guess>100) { print "Guesses must be between 1 and 100!\n"; next; } if ($guess < $goal) { print "Your guess is too low!\n"; } elsif ($guess > $goal) { print "Your guess is too high!\n"; } else #$guess == $goal { print "Hurray! You've got it in $count tries!\n"; print "Enter your name: "; my $name = ; chomp $name; if (exists $scores{$name}) { print "Previous Score for $name was $scores{$name}\n"; } $scores{$name}=$count; #update current score open FILE, '>' ,"high_scores.dat" or die "can't open high score file $!"; foreach (sort{$scores{$b} <=> $scores{$a}}keys %scores) { print FILE "$scores{$name},$name"; } close FILE; $count = 1; print "Playing again, enter quit or exit to stop\n"; next; } $count++; } print "Thanks for playing the game.\n";