use strict; use warnings; my %team; while (defined (my $line=)){ chomp $line; # Zap the newline my ($name,$affiliation) = split /\t/,$line; next unless $affiliation; # Avoid empty lines push @{ $team{$affiliation} }, $name; } # Print the results for my $aff (sort keys %team){ print $aff, " :\t", join(", ", sort @{ $team{$aff} }) , "\n"; } __DATA__ Obi Wan Jedi Yoda Jedi Count Dooku Sith Darth Vader Sith Luke Skywalker Jedi #### Jedi : Luke Skywalker, Obi Wan, Yoda Sith : Count Dooku, Darth Vader