in reply to Changing the format of output

One way is to collect the rs things in an array (@samples) and the AW things in a hash-of-arrays inside your nested for-loops, then print it all out after the loops. UNTESTED:
my @samples; my %aws; foreach my $geno( @ {$genotype} ){ push @samples, $geno->[4]; if(defined $geno->[3]){ push @{ $aws{$geno->[1]} }, $geno->[3]; }#end of if loop to check the genotype defined else{ push @{ $aws{$geno->[1]} }, 'NO'; } }#end of @$genotype print 'Sample,'; print(join ',', @samples); print "\n"; for (keys %aws) { print "$_,"; print(join ',', @{ $aws{$_} }); print "\n"; }