use strict; use warnings; ############################# OP data # dummy items to play with my @animals = (qw( cow camel dog cat )); my @foods = (qw( fish pasture meat )); my @places = (qw( wood sea desert )); # commands my $commands = { select => { completion_list => \@animals, commands => { give => { completion_list => \@foods, }, take_to => { completion_list => \@places, }, }, }, kill => { completion_list => \@animals, } }; ############################# my $branch = { commands => $commands }; my @path; BRANCH: { if ( exists $branch-> { completion_list }) { my $choice = choose( @{ $branch-> { completion_list }}); push @path, $choice } if ( exists $branch-> { commands }) { my $choice = choose( sort keys %{ $branch-> { commands }}); push @path, $choice; $branch = $branch-> { commands }{ $choice }; redo BRANCH } } sub choose { $_[ 0 ]} print "Excellent choice! ===> [ @path ]\n";