my @labels = ('1', '1', '1', '2', '3', '4', '5', '6', '6', '7'); my @seqs = ('a', 'ctgc', 'tggattgactgtg', 'atgcatg' , 'ctgctgcatgtgatgactgtg', 'tgatg', 'gtgt', 'gcgccggactatgattgagctagcgtatgctgcatgctgat', 'gggtttttttttttccccccccccc', 'aaaaaagggggg'); # where the $labels[0] .. [2] all correspond to number 1, so $seqs[0] .. [2] are concatenated to make actgctggattgactgtg etc. I have tried the following code but the problem is that if there are three or more consecutive elements in @labels that have the same value, my code only gets the sequences of two in a row, not all of them once only (meaning that sequences will be redundant). for (my $i=0; $i< @labels; $i++) { if ($labels[$i] == $labels[$i+1]) { # print "$labels[$i] == $labels[$i+1]\n"; push @window_int_seqs, "$seqs[$i]$seqs[$i+1] "; } }