Problem: Given an old and a new org-chart, arrange meetings between old and new team leaders so that for each person in each new team they can hand over HR documents and discuss any current performance issues etc.

Answer 1: Grind out a matrix by hand

Answer 2: Perl!

Submitted as a CUFP for to bringing perl to the realm of pointy haired bosses.

use strict; use warnings; my %old=( # 'Old Team Leader' => ['list','of','persons','reporting','to','this +','team','leader'] 'Beatrice' => ['Charles','Andrei','Ian','Patritzia','Violet','Mart +in','Norbert','Oliver','Mark','Harriet','David'], 'Fran' => ['John','Tracy','Albert','Larry','George','Marvin','Jam +es','Ray'], 'John' => ['Inigo','Kalen','Juliet'], 'Albert'=> ['Dean','Andrew'], 'Edward' => ['Charles','Randall','Robert','Colin'], 'Charles' => ['Shawn','Jon'], 'Daphne' => ['Beatrice','Fran','Wayne','Edward'] ); my %new=( # 'New Team Leader' => ['list','of','persons','reporting','to','this +','team','leader'] 'Charles' => ['Oliver','Norbert','Mark','Robert'], 'David' => ['Newboy','Randall'], 'Edward'=> ['Andrew','Patritzia','Larry','Tracy','Albert'], 'John' => ['Martin','Inigo','Juliet','Kalen'], 'James' => ['Fran','George','Jon','Shawn'], 'Beatrice' => ['Wayne','Charles','David','Edward','Dean','John','J +ames'], 'Edward' => ['Harriet','Ray'] ); my @nobody = (); sub oldreports { @{$old{$_[0]} || \@nobody}; } sub newreports { @{$new{$_[0]} || \@nobody}; } sub oldtl { my $who=shift; for my $lm (keys %old) { for my $rep (oldreports($lm)) { return $lm if ($who eq $rep); } } return ''; } for my $newtl (sort keys %new) { my %needtomeet; for my $rep (newreports($newtl)) { my $oldtl=&oldtl($rep); push @{$needtomeet{$oldtl}},$rep if ($oldtl); } print "\n$newtl needs to meet with "; for my $oldtl (keys %needtomeet) { next if ($oldtl eq $newtl); print "\n\t$oldtl to discuss\n"; for my $reps ($needtomeet{$oldtl}) { for my $rep (@$reps) { next if (($rep eq $newtl) or ($rep eq $oldtl)); print "\t\t$rep\n"; } } } }

Replies are listed 'Best First'.
(jeffa) Re: Restructuring Teams with Perl
by jeffa (Bishop) on Jun 12, 2003 at 00:28 UTC
    You could simplify this code greatly by using a reverse lookup hash to find representatives' leaders:
    my %old_lookup; for my $leader (keys %old) { $old_lookup{$_} = $leader for @{ $old{$leader} }; }
    I don't see the need for @nobody and the oldreports() and newreports() subs. Their function seems to ensure that each value in the %old and %new hashes will be array refs. Well, they are. :) If a leader has no reps, then use an empty anonymous array reference:
    Bill => [],
    That way, the for loop that tries to iterate over the value will gracefully not.

    After you build the lookup table, you can easily populate %needtomeet like so:

    for my $newtl (sort keys %new) { my %needtomeet; for my $rep (@{ $new{$newtl} }) { my $oldtl = $old_lookup{$rep}; push @{$needtomeet{$oldtl}},$rep if $oldtl; yadda yadda ...
    Happy coding, and here's to keeping the PHB at bay. :)

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)