http://www.perlmonks.org?node_id=1035598


in reply to Add space in long string

Using substr as an alternative to regex substitution and also grouping in fours from the right-hand side in case that is a requirement.

$ perl -Mstrict -Mwarnings -E ' sub ins { my $str = shift; my $len = length $str; while ( ( $len -= 4 ) > 0 ) { substr $str, $len, 0, q{ }; } return $str; } say ins( $_ ) for qw{ 1 10 110 1101 10010 101100 1110101 10001011 110100010 };' 1 10 110 1101 1 0010 10 1100 111 0101 1000 1011 1 1010 0010 $

I hope this is useful.

Cheers,

JohnGG