$struct->{g}{a}{t} = 1; $struct->{g}{a}{a} = 1; $struct->{g}{a}{c} = 1; $struct->{g}{t}{c} = 1; $struct->{t}{g}{a} = 1; $struct->{c}{t}{g} = 1; you can then drop out pretty quickly as soon as there isn't a possible completion.