在我的程序中,我有一段代码,根据另一个列表框的值更新列表框。
这样做的代码看起来有点像这样。
$listBox1->bind('<<ListboxSelect>>' => sub {
$listBox2->delete(0, 'end');
for(@{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}}) {
$listBox2->insert('end', $_->name);
}
});

这个很好用。但是,我发现简单地使用列表并在<<ListboxSelect>>上操作列表就更容易了。我已经使用-listvariable将此列表绑定到列表框。
这样做的代码看起来有点像
$listBox1->bind('<<ListboxSelect>>' => sub {
@updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
});

这种方法的问题在于,由于hashOfArraysOfStruct包含结构,所以列表框包含诸如MyStruct=HASH(0x31d7e3c)之类的值。
有没有任何方法可以显示结构MyStruct的MyStruct变量,而无需遍历整个数组并将每个结果单独插入到列表框中?
MCVE
use strict;
use warnings;
use Tk;
use Class::Struct;
struct MyStruct => {
name => '$',
group => '$'
};
my %hashOfArraysOfStruct = (
A => [
MyStruct->new(name => 'Phil', group => 'A'),
MyStruct->new(name => 'Ian', group => 'A'),
MyStruct->new(name => 'George', group => 'A')
],
B => [
MyStruct->new(name => 'Mac', group => 'B'),
MyStruct->new(name => 'Will', group => 'B')
],
C => [
MyStruct->new(name => 'Cath', group => 'C'),
MyStruct->new(name => 'Thom', group => 'C'),
MyStruct->new(name => 'Richard', group => 'C'),
MyStruct->new(name => 'Paul', group => 'C'),
MyStruct->new(name => 'Nick', group => 'C')
]
);
my $mainWindow = MainWindow->new();
my @listOne = sort(keys %hashOfArraysOfStruct);
############################################
#Route One
#Less efficient as has to loop through all the values
#But it displays the name variable of MyStruct
my $listBox1 = $mainWindow->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "single", -listvariable => \@listOne)->pack;
my $listBox2 = $mainWindow->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "single")->pack;
$listBox1->bind('<<ListboxSelect>>' => sub {
$listBox2->delete(0, 'end');
for(@{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}}) {
$listBox2->insert('end', $_->name);
}
});
############################################
############################################
#Route Two
#Works but displays in the form of MyStruct=HASH(0x31d7e3c)
#my @updateableList;
#my $listBox1 = $mainWindow->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "single", -listvariable => \@listOne)->pack;
#my $listBox2 = $mainWindow->Scrolled("Listbox", -scrollbars => "osoe", -selectmode => "single", -listvariable => \@updateableList)->pack;
#$listBox1->bind('<<ListboxSelect>>' => sub {
# @updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
#});
############################################
############################################
#What I would like to happen
#I would like to use route two but when the struct is displayed
#in the list box, instead of being in Route Twos format, it should
#display the name variable of MyStruct.
############################################
MainLoop;发布于 2018-10-04 14:15:11
海量编辑
变化
@updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};至
@updateableList = map { $_->name() } @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};从结构列表中提取名称列表;
https://stackoverflow.com/questions/52648702
复制相似问题