my ($c,$mailcc,$mailto) =(0,'',''); foreach ( @{ $mailref } ){ #put first element in array as mail_to and all others as CC if ($c == 0){ $mailto .= $_; }else{ $mailcc .= $_; } } #### # put first element in array as mail_to and all others as CC my $mailto = shift @$mailref; my $mailcc = join '', @$mailref;