#!/usr/local/bin/perl use strict; use warnings; use Data::Dumper; sub find_numbers { my ($amount, $map)=@_; my $start = 11; my @found; while ($amount--) { while ($map->{$start}) { ++$start; } push @found, $start++; } @found; } my %map = ( '11' => 1, '12' => 1, '15' => 1,); my @free_numbers = find_numbers(4, \%map); die Dumper(\@free_numbers, \%map);