Hello again, corcra,
I’m glad to have been of help.
If I understand you correctly, you now want read the data headings, say:
Field: 0 5 6 7 8 File 1: ['CHROM', ... 'SAMPLE_1A', 'SAMPLE_1B', 'SAMPLE_2A', 'SAMPLE_2 +B'] File 2: ['CHROM', ... 'SAMPLE_1', 'SAMPLE_2', 'SAMPLE_3']
and have the script deduce that File 1 data in fields 5 and 6 should each be compared to File 2 field 5, File 1 data in fields 7 and 8 should each be compared to File 2 field 6, and so on.
That makes the logic more complex, but I don’t know why you think this will be difficult to do line-by-line? Most of the added logic comes before the big while loop:
... my $header1 = <$in1>; <$in2>; my @heads1 = split /\s*,\s*/, $header1; my $index = 5; my %index_map; for (@heads1) { $index_map{$index++} = $1 + 4 if /SAMPLE_(\d+)/; } print $header1; while (my $line1 = <$in1>) { my @fields1 = get_fields($line1); defined(my $line2 = <$in2>) or die "Data missing in file '$file2': $!"; my @fields2 = get_fields($line2); my @out = @fields1; for my $i (5 .. $#fields1) { if ($fields1[$i] ne 'REF') { my $j = $index_map{$i}; $out[$i] = $fields2[$j] if exists $fields2[$j] && $fields2[$j] ne 'REF'; } } @out = map { "'$_'" } @out; print '[', join(', ', @out), "]\n"; } ...
The main addition is a hash (%index_map) to keep track of the correspondences between the fields in File 1 and the matching fields in File 2.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
In reply to Re^3: Can't access data stored in Hash - help!
by Athanasius
in thread Can't access data stored in Hash - help!
by corcra
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |