与perl有HASH参考问题 [英] Having HASH reference issue with perl

查看:452
本文介绍了与perl有HASH参考问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出现以下错误:


名称main :: s只能使用一次:第22行可能出现错字。



不是第5行的HASH参考。



  sub addtwo {

my $ params = shift;

my $ s = $ params-> {structure};

if(ref($ s)eqLIST){

$ c = 0;

foreach $ e(@ {$ s}){
$ s-> [$ c] = addtwo({structures => $ e});
$ c ++;


elsif(ref($ s)eqHASH){

if(标量键%{$ s} == 0){
return undef;
}
else {
foreach my $ e(values%{$ s}){
$ s {$ e} = addtwo({structure => $ s-> ; {$ e}});
}
}
}
else {
$ s = 1;
}

返回$ c;
}

print addtwo(
[
{a => 1,b => 2,c => 3},
{ d => 4,e => 5},
[6,7,8],
9,
10,
11,
[12, 13,14]
]
);


解决方案

Please 在每个的顶部添加 use strict 使用警告'all' >你写的Perl程序。这不是初学者的安全网络:对于任何专业编程来说都是至关重要的。除了%s 以外, t声明 $ c $ e ,并且使用 my ,除非你至少有 use strict 有效

为了提供一个解决方案将重写大部分已放下的内容,所以我认为最好是实施迄今为止给出的建议



但是,请尝试,正如我在我的评论中写的那样,一次只编写一小部分程序,并确保您在添加更多功能时拥有坚实的基础。


Having the following errors:

Name "main::s" used only once: possible typo at line 22.

Not a HASH reference at line 5.

sub addtwo {

    my $params = shift;

    my $s = $params->{structure};

    if ( ref( $s ) eq "LIST" ) {

        $c = 0;

        foreach $e ( @{$s} ) {
            $s->[$c] = addtwo( { structures => $e } );
            $c++;
        }
    }
    elsif ( ref( $s ) eq "HASH" ) {

        if ( scalar keys %{$s} == 0 ) {
            return undef;
        }
        else {
            foreach my $e ( values %{$s} ) {
                $s{$e} = addtwo( { structure => $s->{$e} } );
            }
        }
    }
    else {
        $s = 1;
    }

    return $c;
}

print addtwo(
    [
        { a => 1, b => 2, c => 3 },
        { d => 4, e => 5 },
        [ 6, 7, 8 ],
        9,
        10,
        11,
        [ 12, 13, 14 ]
    ]
);

解决方案

Please add use strict and use warnings 'all' to the top of every Perl program you write. It's not a beginner's safety net: it's essential for any professional programming

As well as %s, you haven't declared $c or $e, and there's very little point in using my at all unless you have at least use strict in effect

To offer a "solution" would be to rewrite most of what you have put down, so I think it's best that you implement the advice that you've been given so far

But please do try, as I wrote in my comment, to write tiny bits of your program at a time, and to make sure that you have a solid basis when you want to add some more functionality

这篇关于与perl有HASH参考问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆