首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Perl中的Stringify

Perl中的Stringify
EN

Stack Overflow用户
提问于 2018-10-04 14:01:50
回答 1查看 96关注 0票数 3

在我的程序中,我有一段代码,根据另一个列表框的值更新列表框。

这样做的代码看起来有点像这样。

代码语言:javascript
复制
$listBox1->bind('<<ListboxSelect>>' => sub {
    $listBox2->delete(0, 'end');
    for(@{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}}) {
        $listBox2->insert('end', $_->name);
    }
});

这个很好用。但是,我发现简单地使用列表并在<<ListboxSelect>>上操作列表就更容易了。我已经使用-listvariable将此列表绑定到列表框。

这样做的代码看起来有点像

代码语言:javascript
复制
$listBox1->bind('<<ListboxSelect>>' => sub {
    @updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};
});

这种方法的问题在于,由于hashOfArraysOfStruct包含结构,所以列表框包含诸如MyStruct=HASH(0x31d7e3c)之类的值。

有没有任何方法可以显示结构MyStructMyStruct变量,而无需遍历整个数组并将每个结果单独插入到列表框中?

MCVE

代码语言:javascript
复制
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;
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-04 14:15:11

海量编辑

变化

代码语言:javascript
复制
@updateableList = @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};

代码语言:javascript
复制
@updateableList = map { $_->name() } @{$hashOfArraysOfStruct{$listBox1->get($listBox1->curselection)}};

从结构列表中提取名称列表;

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52648702

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档