Inserting a space

I am trying to reformat some UK postal codes. I have a csv where field 12 is the postal code. The postal codes length has a maximum of 7.
Basically, I would like a bit of code to look at field 12 and if the postal code has a length of 7, then insert a space into the field fourth from the right, i.e. before - BB102EE, after - BB10 2EE

Help me

i feel kinda retarted for haveing to use 2 substr but i cant think of another way to do the inline edit of $2 at the moment.

#!/usr/bin/perl -w

use strict;

while (<>) {
if (/(\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,\w*,)(\w*)(.*)/) {
        if (length($2) == 7) {
                my $zipstart=substr($2,0,4);
                my $zipend=substr($2,4,3);
                print "$1$zipstart $zipend$3\n";
                next;
        }

        print "$1$2$3\n";
}
}

The below is what the perl group said.

When working with long, delimited lists of data like
that, I'm inclined to split it, twiddle it, then
reassemble it.

while (<>) {
 my @data = split /,/;
 #use substr to substitute in a space at the 4th
character if our string is 7 characters long
 substr($data[11], 4, 0, " ") if length $data[11] ==
7;
 print join(',', @data); #no need for a newline, we
have one at the last position
}

if you want, you could use $, set to ',' and then just
print @data at the end. Might be a little faster, but
I prefer being explicit.

That should work, but I didn't test it, so it may need
a little twiddling.

But you also caught my curiousity...what's with a 7
digit zipcode and what would the space do? I'm only
familiar with 5 digit, 9 digit, and 11 digit (DPBC)
codes (and none of those have spaces). I'm thinking
canadian postal codes, but I'm not sure.

-Jim......

Using awk...

awk '
BEGIN{FS=OFS=","}
{
  if (length($12)==7) {
    $12=substr($12,1,4) " " substr($12,5,3);
  }
  print $0;
}
' file1

Thanks very much for your responses. It's been a great help.

Thanks again