scripting issue

folks;
i'm trying to write a shell script to do the following:

  1. i have a file with 39 fields, i'm trying to add 10 more fields with (!) as a field separator. with the following requirement:
    if field number 20 has a value, then field number 40 will show as (!M!), and if the field number 20 does not have any value, then field number 40 will show (!E!).

any help

#!/usr/local/bin/perl

#////open files for read and write
my $file = $ARGV[0] || "-"; # first file
open FP,"< $file" or die "Can't open $file : $!";
open(NF, "> out.txt");


#////MAIN////
#print to file and screen
while($line = <FP>){

  @fields = split(/!/, $line);

  	if(@fields[20] =~ /[0-9]/){
	  	$fields[40] = "M";
  		$line =~ s/\n/!M!/;
  		print( NF $line);
	}else{
  		$fields[40] = "E";
  		$line =~ s/\n/!E!/;
  		print( NF $line);
	}

	print ( NF "\n");
	
	foreach $value (@fields){
    	print $value;
	}

  	print"\n";

}
close(FP);
close(NF);

comand line:

perl script_name.pl in.txt

thanks; i'll try it today