my $line = 'atccatccctttaat'; my %triplets; for (0 .. 2) { @{$triplets{$_}} = unpack(('x' x $_).'(a3)*',$line); pop @{$triplets{$_}} if length($triplets{$_}->[-1]) != 3; print "Offset $_: @{$triplets{$_}}\n"; }