To jump to the line of the manager you need to know where it is, this means that you have to loop through the whole data once to find the manager's index. So if you do that, you might as well go through the file once, and fill the holes as you loop through the data to print it in a file. Something like:

for (@line) { $employee = GetElement($_); $groups{$employee->[name]} = $employee->[group]; push @list, $employee; } for (@list) { $_->[group] = $groups{$_->[manager]} unless $_->[group]; print ToCsv($_); }

Or, in the spirit of TIMTOWTDI, you can use references so that the group of an employee and their manager actually refer to the same scalar, even if it is still unknown:

use Data::Dumper; my %people; while (<DATA>) { chomp; my ($name, $group, $manager) = split/,/; # Don't do that, use Text:: +CSV instead if ($group) { ${ $people{$name}{Group} } = $group; # Autovivification means: if +the reference exists, change the value, else, create a new reference } else { $people{$name}{Group} = \${ $people{$manager}{Group} }; # Autovivi +fication again } } print Dumper \%people; print ${ $people{"Woody Boyd"}{Group} }; __DATA__ Sam Malone,Cheers,Rebecca Howe Woody Boyd,,Rebecca Howe Rebecca Howe,Cheers,Rebecca's Manager

Edit: You all recognized TIMTOWTDI desguised as TIMTOWTDIT :)


In reply to Re: Field data missing; copying from other field by Eily
in thread Field data missing; copying from other field by pbassnote

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.