#!/usr/bin/perl -w use strict; open RND, "rand_sorted.txt" or die "no file found"; my @rnd; @rnd = grep {chomp;} ; close RND; WILLOP: while (<>){ chomp; FORLOP: foreach my $i (@rnd){ if ($i != $rnd[$#rnd]){ if ($. == $i){ print "$_\n"; shift @rnd; last FORLOP; } else { print "$_\n"; last FORLOP; } }