$dlm = '~'; $str = 'my~mind~has~been~broken'; foreach (split(/$dlm/,$str)) { push(@ary,$_,$dlm); } $ary[$#ary--]; print join(',',@ary),"\n";