Perl 中的字符串化结构
Stringify Struct in Perl
在我的程序中,我有一段代码根据另一个列表框的值更新一个列表框。
执行此操作的代码看起来有点像这样。
$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
的 name
变量而不用遍历整个数组并将每个结果单独插入列表框中?
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;
大量编辑
改变
@updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
至
@updateableList = map { $_->name() } @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
从结构列表中提取名称列表;
在我的程序中,我有一段代码根据另一个列表框的值更新一个列表框。
执行此操作的代码看起来有点像这样。
$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
的 name
变量而不用遍历整个数组并将每个结果单独插入列表框中?
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;
大量编辑
改变
@updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
至
@updateableList = map { $_->name() } @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
从结构列表中提取名称列表;