#!/usr/bin/perl use strict; use warnings; # start with an array of arrays (list of lists, which can come from a data file): # each row contains all the "synonymous" names in a set, and # each element in the row becomes a hash key whose value is # a reference to the whole row/set of synonymous names: my %altnames; # will be a HoA while () { my @nameset = split; for my $name ( @nameset ) { if ( ! exists( $altnames{$name} )) { $altnames{$name} = \@nameset; } else { my $newname = $name; $newname .= " " while ( exists( $altnames{$newname} )); $altnames{$newname} = \@nameset; } } } for my $name ( qw/Allen Bill Chuck Dave Edward Jan/ ) { my $modifier = ''; for ( grep /^$name\s*$/, keys %altnames ) { print "The name $_ is $modifier a member of the set @{$altnames{$_}}\n"; $modifier = 'also'; } } __DATA__ Allen Al Charles Chuck Chas David Dave Edward Eddie Ed Janet Jan Janice Jan William Will Willie Bill Billie