my %nums = qw(A 0 A# 1 BB 1 B 2 C 3 C# 4 DB 4 D 5 D# 6 EB 6 E 7 F 8 F# 9 GB 9 G 10 G# 11 AB 11); my %notes = qw(0 A 1 A# 2 B 3 C 4 C# 5 D 6 D# 7 E 8 F 9 F# 10 G 11 G#); use List::Util qw(max); sub find_root { my @f; foreach my $n (map {$nums{uc($_)}} @_) { ++$f[($n+$_)%12] foreach (0,0,0,2,5,5,8,10); } my $max = max(@f); map {$notes{$_}} grep {$f[$_] == $max} (0..11); } print find_root(qw(c f a)),"\n"; # F - 2nd inversion print find_root(qw(e g a# c)),"\n"; # C - 1st inversion 7th) print find_root(qw(f b d# g#)),"\n";# C#(!)/G# - 'Tristan' chord, C#9(no bass) *or* G#m6