#!/usr/bin/perl use strict; use warnings; use Tie::File; my %hash = (); my $file = '/path/to/file.txt'; my $count = 700; tie my @array,'Tie::File',$file or die $!; while(keys(%hash) < $count-1){ $hash{$array[int rand(scalar(@array)-1)]} = 1; } untie @array; print scalar(keys(%hash)); print $_,"\n" for(keys(%hash));