my @with; my @without; foreach (@arr) { if (/^\@(.*)/) { push(@with,$1); } else { push(@without,$_); } } my $str1 = join(",",@without); my $str2 = join(",",@with);