in reply to replacing one or more newlines with commas

I don't see why you need to substitute \n with commas to start with, if you're going to split the strings....

foreach my $text ( @test ) { my @elements = split( /\n/, $text ); # do something with @elements }

... would suffice, no?