代码之家  ›  专栏  ›  技术社区  ›  chickeninabiscuit

Perl编译难题

  •  0
  • chickeninabiscuit  · 技术社区  · 15 年前

    我正在编译这个方法:

    #changes the names of the associations for $agentConf
    #where the key value pairs in %associationsToChangeDict are the old and new values respectively
    sub UpdateConfObjectAssociations{
        my($agentConf, %associationsToChangeDict) = @_;
    
        foreach my $association ($agentConf->GetAssociations()) {
            if ( grep {$_ eq $association->Name()} keys %associationsToChangeDict) {
                my $newValue = %associationsToChangeDict{$association->Name()};
                $association->Value($newValue);
            } 
        }   
    }
    

    syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
    .pm line 75, near "%associationsToChangeDict{"
    syntax error at D:\Install\AutoDeployScripts\scripts\Perl/.\AI\SiteMinderHelper
    .pm line 79, near "}"
    

    有人知道问题在哪里吗?

    1 回复  |  直到 15 年前
        1
  •  7
  •   Axeman maxelost    15 年前

    是的,您可以从这样的散列中获取一个片段(即多个值):

    my @slice = @hash{ @select_keys };
    

    my $value = $hash{ $key };
    

    但是不能用“%”开头的sigil来处理哈希。缺少Perl6是没有意义的(sigils不会根据数字变化)。

    因为您希望散列中有一个项,所以您的分配应该是:

    my $newValue = $associationsToChangeDict{ $association->Name() };
    

    Perl中有三个上下文, 空的 , ,和 列表 上下文 而不是变量名的一部分。我们看到一个 空的 sub -s、 当程序员只想完成某件事,而不关心是否返回值时。

    只剩下 标量 当谈到变量时。这种工作就像语言中的单数和复数形式。由于Larry Wall在设计Perl时受到了自然语言的影响,这些并行是自然的。但是没有“散列上下文”。当然,要稍微复杂一点,有些东西在标量上下文中包装时被评估为列表,也有上下文意义,它只是评估结果列表的大小。

    你不可能这样做(但它有意义):

    my $count = @list[1..4];
    

    但你可以这样做:

    my $count = ( grep { $_ % 2 == 0 } @list[ @subscripts ] );
    

    grep 可能聪明到足以计算成功,而不是形成一个新的列表

    推荐文章