Here is some idiomatic code to help get you to where I think you are headed:
#!/usr/bin/perl use warnings; use strict; use Data::Dumper; my $filename = 'pedigree_proband_testfile.txt'; my %n = (birth_year=> 13, motherID=> 4, individualID=> 2, gender => +5); my %mominfo; open my $f, "<", $filename or die "ERROR: Cannot open $filename:$!"; while (<$f>){ chomp; my @c = split /\t/, $_; my $currentMomID = $c[ $n{motherID} ] || $c[ $n{individualID} ] or +die "No Mother ID in record nbr $.=$_;"; my $currentMom = $mominfo{ $currentMomID } ||= {}; # Create an empty + mom if she does not exist if ($currentMomID == $c[ $n{individualID} ]){ print "This is Mom ($currentMomID)'s main record: $_\n"; $currentMom->{birth_year} = $c[ $n{birth_year} ]; }else{ push @{ $currentMom->{CHILDREN} }, {ID=> $c[ $n{individualID} ] , + birth_year => $c[ $n{birth_ye +ar} ], gender => $c[ $n{gender} ]}; } } print Dumper \%mominfo;
Output:
This is Mom (2)'s main record: BC000 0 2 M + 999 $VAR1 = { '3' => { 'CHILDREN' => [ { 'gender' => 'F', 'birth_year' => '51', 'ID' => 1 }, { 'ID' => 4, 'birth_year' => ' 50', 'gender' => 'M' }, { 'gender' => 'F', 'birth_year' => '47', 'ID' => 5 }, { 'birth_year' => undef, 'gender' => 'F', 'ID' => 6 }, { 'gender' => 'M', 'birth_year' => '', 'ID' => 7 }, { 'gender' => 'F', 'birth_year' => '42', 'ID' => 8 }, { 'ID' => 9, 'birth_year' => '39', 'gender' => 'F' }, { 'birth_year' => '35', 'gender' => 'F', 'ID' => 10 } ] }, '8' => { 'CHILDREN' => [ { 'ID' => 11, 'gender' => 'M', 'birth_year' => ' 11' }, { 'birth_year' => ' 9', 'gender' => 'F', 'ID' => 12 } ] }, '2' => { 'birth_year' => '' }, '36' => { 'CHILDREN' => [ { 'ID' => 3, 'gender' => 'F', 'birth_year' => ' 65' } ] } };

                Memory fault   --   brain fried


In reply to Re: How to map data from one column based on another column in perl by NetWallah
in thread How to map data from one column based on another column in perl by waekit

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.