$a = '12345678901'; while ( $a =~ /(\d{3})/g ) { push ( @nums, $1 ); $last = $'; } if ( length($a) % 3 ) { push ( @nums, $last ); }