Arrays 引用哈希->;其成员数组->;这个数组';s成员';s值。如何优雅地访问和测试?
我想用一个像Arrays 引用哈希->;其成员数组->;这个数组';s成员';s值。如何优雅地访问和测试?,arrays,perl,hash,reference,Arrays,Perl,Hash,Reference,我想用一个像 @{ %$hashref{'key_name'}[1] 或 获取并测试由我的hash保存的数组(引用)的第二个(index=1)成员作为其“key_name”的值。但是,我不能 这里的代码是正确的(它可以工作),但我希望将我标记的两行代码组合成一行高效的perl优雅的代码 foreach my $tag ('doit', 'source', 'dest' ) { my $exists = exists( $$thisSectionConfig{$tag});
@{ %$hashref{'key_name'}[1]
或
获取并测试由我的hash保存的数组(引用)的第二个(index=1)成员作为其“key_name”的值。但是,我不能
这里的代码是正确的(它可以工作),但我希望将我标记的两行代码组合成一行高效的perl优雅的代码
foreach my $tag ('doit', 'source', 'dest' ) {
my $exists = exists( $$thisSectionConfig{$tag});
my @tempA = %$thisSectionConfig{$tag} ; #this line
my $non0len = (@tempA[1] =~ /\w+/ ); # and this line
if ( !$exists || !$non0len) {
print STDERR "No complete \"$tag\" ... etc ... \n";
# program exit ...
}
我知道你(将军“你”)可以优雅地结合这两条线。有人能告诉我怎么做吗
这段代码用于测试配置文件的一个部分,该部分已被config::Simple读入$thisSectionConfig对-a-hash的引用。然后,每个配置文件key=value对(我使用datadumper查看)都作为两个成员数组保存:[0]是键,[1]是值。$tag是配置设置,必须出现在此代码段处理的配置文件部分中
谢谢你的帮助 你应该读一下。我猜你想要这样的东西:
foreach my $tag ('doit', 'source', 'dest') {
if(exists $thisSectionConfig -> {$tag}){
my $non0len = ($thisSectionConfig -> {$tag} -> [1] =~ /(\w+)/) ;
}
else {
print STDERR "No complete \"$tag\" ... etc ... \n";
# program exit ...
}
foreach my $tag ('doit', 'source', 'dest') {
if(exists $thisSectionConfig -> {$tag}){
my $non0len = ($thisSectionConfig -> {$tag} -> [1] =~ /(\w+)/) ;
}
else {
print STDERR "No complete \"$tag\" ... etc ... \n";
# program exit ...
}