Perl MindMelting来自数组异常的标量中的未初始化变量(Perl MindMelting uninitialized variable in scalar from array excep

系统教程 行业动态 更新时间:2024-06-14 17:01:34
Perl MindMelting来自数组异常的标量中的未初始化变量(Perl MindMelting uninitialized variable in scalar from array exception)

我很难说出这个问题,因为我真的难以理解可能导致这个错误的原因。

当我输入这样的代码时:

my @strArray= ('TypeKey', 'AccidentType', '01'); my %HashArrayThing = ('EnglishArray' => @strArray, 'FrenchArray' => ('julier', 'aout', 'septembre')); my $scalar = @HashArrayThing{EnglishArray}; my @ARR = @HashArrayThing{EnglishArray}; say $scalar, say $ARR[0];

输出是:

TypeKey TypeKey1

当我只改变say $ARR[0] say $ARR[1] (或任何其他数字)时,我得到错误:“在第50行说未初始化的值”(与'说$ scalar'相同的行)

这对我没有意义。 我可以理解$ ARR [1]可能超出范围,可能olny从存储在hashset中的数组中获得了第一个值,但我完全不了解它的来源。 我希望那里有一个人能够理解perl谁可以启发我。

PS。 同样来自'1'的地方是bieng添加到typekey。

I had a hard time titleing this question, as I am truly stumped as to what could be causing this error.

when I have the code typed as such:

my @strArray= ('TypeKey', 'AccidentType', '01'); my %HashArrayThing = ('EnglishArray' => @strArray, 'FrenchArray' => ('julier', 'aout', 'septembre')); my $scalar = @HashArrayThing{EnglishArray}; my @ARR = @HashArrayThing{EnglishArray}; say $scalar, say $ARR[0];

the output is:

TypeKey TypeKey1

when I change ONLY the say $ARR[0] to say $ARR[1] (or any other number) I get the error: "Uninitialized value in say at line 50"(same line as the 'say $scalar')

This makes no sense to me. I could understand that the $ARR[1] could be out of bounds having maybe olny gotten the first value from the array stored in the hashset, but I don't understand at all where this is coming from. I hope there is someone out there with a thourough understanding of perl who can enlighten me.

PS. also where is the '1'coming from that is bieng added to typekey.

最满意答案

这一行:

my %HashArrayThing = ('EnglishArray' => @strArray, 'FrenchArray' => ('julier', 'aout', 'septembre'));

实际上是在创建这个结构:

( 'AccidentType' => '01', 'FrenchArray' => 'julier', 'EnglishArray' => 'TypeKey', 'aout' => 'septembre' )

我想象你在哪里:

( 'FrenchArray' => ['julier', 'aout', 'septembre'], 'EnglishArray' => ['TypeKey', 'AccidentType', '01'], )

发生这种情况是因为哈希中键/值对的值必须是SCALAR。 所以它是一个简单的值,如数字或字符串或对Hash或Array的引用

以下是初始化Hash的正确方法:

my %HashArrayThing = ( 'EnglishArray' => [@strArray], 'FrenchArray' => ['julier', 'aout', 'septembre'] );

请注意,先前传入的数组现在包含在[] ,这会创建对数组的引用。

This line:

my %HashArrayThing = ('EnglishArray' => @strArray, 'FrenchArray' => ('julier', 'aout', 'septembre'));

is actually creating this structure:

( 'AccidentType' => '01', 'FrenchArray' => 'julier', 'EnglishArray' => 'TypeKey', 'aout' => 'septembre' )

where I imagine you were expecting:

( 'FrenchArray' => ['julier', 'aout', 'septembre'], 'EnglishArray' => ['TypeKey', 'AccidentType', '01'], )

This is happening because the value of a key/value pair in a Hash must be a SCALAR. So it is a simple value like a number or a string or a reference to Hash or an Array.

The following is the correct way to initialize your Hash:

my %HashArrayThing = ( 'EnglishArray' => [@strArray], 'FrenchArray' => ['julier', 'aout', 'septembre'] );

Notice that the arrays you passed in previously are now wrapped in [] which creates a reference to an array.

更多推荐

本文发布于:2023-04-20 18:39:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/1c470069ff26360c6ee5d278bc9b972a.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:标量   数组   初始化   变量   异常

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!