$a[0] = 'CHARLIE ROOT'; $a[1] = 'HARRY NODE'; s/([$a[~$i]])/$1 eq ' '?' ':''/eg and print while ($i, $_) = each @a;