# this opens the answers file for append open(ANSFILE,">>answers.txt") or die "Couldn't open answers for appending: $!\n"; # rather than a while loop and a variable just use a foreach # loop with a range ... $_ will contain the number foreach(1 .. 10) { print $questions[ int( rand( scalar(@questions) ) )]; my $ans = uc(); print ANSFILE $ans; } close(ANSFILE); exit;