use strict; use warnings; my $input = '1234562234563334564444565555566789'; my @sets_of_six; while (length $input > 6){ my $front = substr($input, 0, 6); $input = substr($input, 6); push @sets_of_six, $front; } push @sets_of_six, $input if length $input > 0; use Data::Dumper; print Dumper( @sets_of_six );