$names12[$i]=@myNames[$i].','; #should be $names12[$i]=$myNames[$i].','; #### # execute as perl -n program.pl < myTextFile #!/usr/bin/perl # get the words from current text block and push into array.... while (/(\w+)/g) { push @names12, $1; } END { # your array contains words, now add comma to printout print join(',', @names12); }