use strict; my $tempStr = 'BE65826000483296'; my $tempStr1; while ( $tempStr . length ) { $tempStr1 .= " " . substr( $tempStr, 0, 4 ); $tempStr = substr( $tempStr, 4 ); } $tempStr1 =~ s/^\s//isg; print $tempStr1;