#!/usr/local/bin/perl use strict; use warnings; srand; # moved subroutine calls to _after_ their declarations, not necessary, but a good # habit that can avoid nasty bugs in more complicated scenarios. # this also means that input being an auxilliary sub to guess_routine needs to go # before it sub input { my $arrayref=shift; print "\nSo far you've guessed @$arrayref." if $arrayref; print "\nWhat is your guess?\n"; my $in=; chomp ($in); $in } sub guess_routine { my $done=0; until ($done) { my @guesses; print "I have a number from 0 to 19 inclusive.\nTry to guess it.\n"; my $numb=int(rand(20)); my $in = input(); while ($in != $numb){ print($in > $numb ? 'Too high! ' : 'Too low! '); # moved to trinary operator, just shrinks up # code, if else isn't bad @guesses = sort{$a <=> $b} (@guesses,$in); # you were pushing onto the array, then sorting # simply sorting array and value together shoule # be more efficient $in = input(\@guesses); # created a subroutine for # this, since you had similar code in two places. } print "\nYes, $numb is the number!\n"; print "So you want another go?\n"; my $answer=; # no chomp, you check /^y/ chomp unecessary if ($answer !~/^y/i){ print "\nOK, bugger off then!\n"; $done=1; } } } guess_routine(); #moved the print and rand into the routine # that way you don't have the code duplicated when user # says y for new game...