I'm struggling to understand why if you want to 'merge' records from 3 files you are creating hash keys containing the filename ?

$fields[2] = $file; my $locus = join "\t", @fields[0,1,2,3,4,5];

From this output code, it looks like you want to create a 'cross tab' with HET/HOM derived from each file in a different column. Is that correct ?

my $samples_list = join "\t", @samples; print OUT "CHROM POS ID REF ALT QUAL $samples_list

update : my best guess at what you are trying to do.
#!perl use strict; use warnings; use Data::Dump 'pp'; # get .var files in current directory my $path = '.'; opendir(DIR, $path); my @files = sort grep { /\.var$/ } readdir(DIR); closedir(DIR); # remove _suffix and .var extension my @samples = map { m/^([^_]+).*\.var/ ; $1 } @files; print "Samples are @samples\n"; # scan files and build hash my %zygos = (); my %info = (); my @header= (); foreach my $i (0..$#files) { my $file = $files[$i]; my $count = 0; print "Reading $file .. "; open IN, '<', $file or die "Could not open $file :$!"; while (<IN>) { chomp; my @fields = split /\t/, $_; # extract header line if (/^#/){ @header = @fields; next; } # pull out genotype my ($g,undef) = split ":",$fields[9]; my $zyg = ($g eq '0/1') ? "HET" : "HOM"; # create hash key from first 6 fields my $id = join "\t", @fields[0..5]; # build hashes $info{$id} = join "\t", @fields[10..$#fields]; @{$zygos{$id}}[$i] = $zyg; ++$count; } print "$count lines\n"; } close IN; print "Dump of \%zygos hash\n"; pp \%zygos; # open output file my $outfile = ($ARGV[0] || '').'merged.xls'; open OUT, '>', $outfile or die "Could not open OUTFILE"; # print header my $col_header = join "\t", @header[0..5],@samples,@header[10..$#heade +r]; print OUT $col_header."\n"; # print records foreach my $id (sort keys %info) { # replace undef with '' my @zygos = map { $_ || ''} @{$zygos{$id}}[0..$#samples]; print OUT join "\t",$id,@zygos,$info{$id}; print OUT "\n"; }
poj

In reply to Re^3: error returning hash value? by poj
in thread error returning hash value? by drlecb

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.