我很难说出这个问题,因为我真的难以理解可能导致这个错误的原因。
当我输入这样的代码时:
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 TypeKey1when 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.
更多推荐
发布评论