#!/usr/bin/perl use 5.014; # 991184 my @positions; my @data = ('1 ACAC', '2 AGAC', '3 AGTC', '4 ACCA', '5 DUMMY', '6 ACAATG', '7 CAAC', '8 acaacc', '9 aca ', ); for my $data(@data) { if ( $data =~ /[GT]/ || $data =~ /[^[ACTG]{4}$/) { say "Data ERROR or contains G or T ( $data )"; next; } elsif ( $data =~ /^\d+ [AC]{4}$/i ) { # too many; too few? covered here $data =~ s/ [AC]{4}//i; chomp $data; push @positions, $data; } else { say "Problem with data? $data"; } } print "\n Good data at positions: "; for my $position (@positions) { print "$position "; # depending on size of valid positions, you may } # want to stack them vertically -- # simply replace the 'print' with 'say' say "\n Done";