%sections = (sec1 => 2, sec2 => 4, sec3 => 4, sec4 => 3, ...); %students = (studentA => [sec3, sec1, sec2], studentB => [sec1], studentC => [sec4, sec3, sec1], studentD => [sec2, sec3], studentE => [sec2, sec4, sec3, sec1], ...); %matches = best_permutation(\%sections, \%students);