%hash = ('1' => cat', '3'=> 'mouse', '5'=>'whale' ); if $hash{2} defined then return $hash{2} else return next element sorted by hash key, in this case, $hash{3}