my $tbltext = qq(
| a1 | a2 | a3 | a4 |
| b1 | b2 | b3 | b4 |
| c1 | c2 | c3 | c4 |
);
sub fix_column {
my $s = shift;
my $nth = shift;
my $counter = 0;
$s =~ s/()/
++$counter == $nth #is this the nth cell?
? "${1}newtext" #if yes, add "newtext"
: $1 #otherwise, leave it
/ge;
return $s;
}
$tbltext =~ s/()(.*?)(<\/tr>)/$1.fix_column($2, 3).$3/ge;
print "$tbltext\n";
__END__
| a1 | a2 | a3 | newtexta4 |
| b1 | b2 | b3 | newtextb4 |
| c1 | c2 | c3 | newtextc4 |