Hello corcra, and welcome to the Monastery!

You begin by reading the whole of file 2, and saving all the data you will need later when processing file 1. This is inefficient, and may be problematic if file 2 is large. A better strategy is to read the two input files together, one line at a time:

#! perl use strict; use warnings; my $file1 = 'File1.txt'; my $file2 = 'File2.txt'; open(my $in1, '<', $file1) or die "Cannot open file '$file1' for reading: $!"; open(my $in2, '<', $file2) or die "Cannot open file '$file2' for reading: $!"; print scalar <$in1>; <$in2>; 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' && $i <= $#fields2 && $fields2[$i] ne 'REF') { $out[$i] = $fields2[$i]; } } @out = map { "'$_'" } @out; print '[', join(', ', @out), "]\n"; } close $in2 or die "Cannot close file '$file2': $!"; close $in1 or die "Cannot close file '$file1': $!"; sub get_fields { my ($line) = @_; chomp $line; my @fields = split /\s*,\s*/, $line; s{ ^ \[? ' }{}x for @fields; s{ ' \]? $ }{}x for @fields; return @fields; }

Output:

13:39 >perl 959_SoPW.pl ['CHROM', 'POS', 'REF', 'ALT', 'LIST', 'SAMPLE_1A', 'SAMPLE_2A', 'SAMP +LE_3A'] ['M', '16', 'T', 'C', 'C', 'REF', 'C', 'REF'] ['M', '381', 'T', 'A', 'A', 'A', 'REF', 'REF'] ['M', '529', 'A', 'G', 'G', 'REF', 'G', 'REF'] 13:39 >

Note: In the above code I’ve assumed that the data files are formatted as you’ve shown. But if (as I half suspect) they are actually formatted as proper CSV files, then you will be better served reading them with one of the modules designed for this purpose, such as Text::CSV_XS.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: Can't access data stored in Hash - help! by Athanasius
in thread Can't access data stored in Hash - help! by corcra

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.