foreach $_ (@total) { my @line_elems; for($i = 0; $i < $sampleSize; $i++){ ... push @line_elems, $temp; } print (join "\t", @line_elems) ."\n"; }