Does your program need to be terminal based or can you use an graphical gui? For instance, if you can use Gtk2, you can do something like this.
#!/usr/bin/perl -w
use strict;
use Gtk2 -init;
use Gtk2::SimpleList;
# by muppet on gtk-perl maillist
my $window = Gtk2::Window->new;
$window->signal_connect (destroy => sub { Gtk2->main_quit });
my $hbox = Gtk2::HBox->new;
$window->add ($hbox);
my $s1 = Gtk2::SimpleList->new (
'Number' => 'int',
'Function' => 'text',
'Default' => 'int',
'Subvalues' => 'scalar',
);
# Hide the Subvalues column. This will remove it from the treeview,
# not from the model. One alternative to this is to use a column of
# SimpleList type 'hidden', but that creates a column which only holds
# text strings. Yet another alternative is to create a new SimpleList
# column type for holding scalars in a hidden column... but this is
# easier. :-/
$s1->remove_column ($s1->get_column (3));
my $s2 = Gtk2::SimpleList->new (
'Value' => 'int',
'Midpoint' => 'int',
'Description' => 'text',
);
@{ $s1->{data} } = (
[1, 'Volume', 0, make_sub_list ([ 32, 128, 'Preset1'], [ 220,
128, 'Preset2'])],
[2, 'Pan', 128, make_sub_list ([ 64, 128, 'position in the
stereo field'])],
[3, 'Treble', 128, make_sub_list ([196, 128, 'high frequencies'])],
[4, 'Mid', 128, make_sub_list ([157, 128, 'midrange
frequencies'])],
[5, 'Bass', 128, make_sub_list ([200, 128, 'low frequencies'])],
[6, 'Color1', 128, make_sub_list ([10,5,'White'],[20,15,'Blue'])],
[7, 'Whee', 255, make_sub_list ([10,5,'Ready'],[15,20,'Set'],
[42,-1,'Go'])],
);
$hbox->add ($s1);
$hbox->add ($s2);
$s1->get_selection->signal_connect (changed => sub {
my ($selection) = @_;
my $slist = $selection->get_tree_view;
my ($sel) = $slist->get_selected_indices;
# Here's something a little bit evil... we've stored the subvalues
# as the TiedList objects that SimpleList uses. However, we need the
# underlying ListStore for the TreeView. The ListStore is stored in
# the TiedList object, so we have to get to that with tied(). Note
# that we had a reference to the tied object, so we have to
# dereference that when calling tied(), or it doesn't work.
my $tiedlist = $slist->{data}[$sel][3];
$s2->set_model (tied(@$tiedlist)->{model});
# If you actually want to be able to use $s2->{data}, we have to
# update it separately. Perhaps we should override set_model()
# on SimpleList?
$s2->{data} = $tiedlist;
});
$window->show_all;
Gtk2->main;
sub make_sub_list {
my $store = Gtk2::ListStore->new (qw(Glib::Int Glib::Int
Glib::String));
# Note this nifty trick for filling the list...
tie my @a, 'Gtk2::SimpleList::TiedList', $store;
@a = @_;
return \@a;
}