$infile="hosts.txt"; open(INPUT, $infile) || die "Error opening $infile\n"; my @allhosts = ; close INPUT; while ($approvhost=) { chop($approvedhost); # or it won't partially match # your hosts entries becaus of # the training \n unless ($approvedhost) { last; } # so you can quit # with a null entry foreach $host (@allhosts) # omit the <> you're not # reading a file or stdin { if ($host =~ m/^$approvhost/) { print $host; } } }