参考数组大小下降到一个元素(Referenced array dropped in size to one element)

编程入门 行业动态 更新时间:2024-10-26 14:28:44
参考数组大小下降到一个元素(Referenced array dropped in size to one element)

亲爱的perl程序员,

我想访问这个数组

my @vsrvAttribs = qw( Code Description vsrv_id vsrv_name vsrv_vcpu_no vsrv_vmem_size vsrv_vdspace_alloc vsrv_mgmt_ip vsrv_os vsrv_virt_platf vsrv_owner vsrv_contact vsrv_state );

通过由变量和字符串后缀组成的变量,这当然会导致这样的错误消息

Can't use string ("@vsrvAttribs") as an ARRAY ref while "strict refs" in use at cmdbuild.pl line 262.`

因此我决定通过哈希来获得对数组的引用

my %attribs = ( vsrv => @vsrvAttribs );

这是我需要获取上述数组的内容的代码

foreach my $classTypeKey (keys %classTypes) { my @attribs = $attribs{$classTypeKey}; print Dumper(\@attribs); }

看来我可以获得对数组@vsrvAttribs的引用,但是当我使用Dumper检查数组的内容时,该数组只有一个元素

$VAR1 = [ 'Code' ];

你有什么想法在哪里可能是问题?

Dear fellow perl programmers,

I wanted to access to this array

my @vsrvAttribs = qw( Code Description vsrv_id vsrv_name vsrv_vcpu_no vsrv_vmem_size vsrv_vdspace_alloc vsrv_mgmt_ip vsrv_os vsrv_virt_platf vsrv_owner vsrv_contact vsrv_state );

through a variable composed of a variable and a string suffix, which of course led to the error message like this

Can't use string ("@vsrvAttribs") as an ARRAY ref while "strict refs" in use at cmdbuild.pl line 262.`

Therefore I decided to get the reference to the array through a hash

my %attribs = ( vsrv => @vsrvAttribs );

And this is the code where I need to get the content of aforementioned array

foreach my $classTypeKey (keys %classTypes) { my @attribs = $attribs{$classTypeKey}; print Dumper(\@attribs); }

It seems I can get the reference to the array @vsrvAttribs, but when I checked the content of the array with Dumper , the array have got only one element

$VAR1 = [ 'Code' ];

Do you have any idea where could be the problem?

最满意答案

如何将数组存储在散列中并稍后访问?

你需要像这样通过引用来存储你的数组:

my %attribs = ( vsrv => \@vsrvAttribs );

请注意@ sigil之前的反斜杠。 这告诉perl你想要一个数组的引用。

然后,当访问存储在$attribs{vsrv}的数组时,您需要将其视为引用而不是数组。 你会做这样的事情:

foreach my $classTypeKey (keys %classTypes) { # make a copy of the array by dereferencing my @attribs = @{ $attribs{$classTypeKey} }; # OR just use the array reference if profiling shows performance issues: my $attribs = $attribs{$classTypeKey} # these will show the same thing if you haven't done anything to @attribs # in the interim print Dumper(\@attribs); print Dumper($attribs); }

你为什么只获得一个价值,其余的阵营去哪里?

您从@vsrvAttribs丢失的值不会丢失,它们被分配为%attribs本身的键和值。 在您完成任务后尝试添加以下内容,您将自己看到:

my %attribs = ( vsrv => @vsrvAttribs ); print Dumper(\%attribs);

你会看到这样的输出:

$VAR1 = { 'vsrv_contact' => 'vsrv_state', 'vsrv_virt_platf' => 'vsrv_owner', 'vsrv' => 'Code', 'vsrv_name' => 'vsrv_vcpu_no', 'vsrv_mgmt_ip' => 'vsrv_os', 'Description' => 'vsrv_id', 'vsrv_vmem_size' => 'vsrv_vdspace_alloc' };

这是因为perl通过将内容@vsrvAttribs作为多个参数展开到列表文字()来解释您的任务:

my %attribs = ( # your key => first value from array vsrv => 'Code', # subsequent values of the array Description => 'vsrv_id', vsrv_name => 'vsrv_vcpu_no', vsrv_vmem_size => 'vsrv_vdspace_alloc', vsrv_mgmt_ip => 'vsrv_os', vsrv_virt_platf => 'vsrv_owner', vsrv_contact => 'vsrv_state', );

这在perl中是合法的,并且你可能想要这样做的理由是有的,但在你的情况下它不是你想要的。

顺便提一句,如果你的数组中有偶数个元素,你将会被警告说perl正在做一些你可能不想要的东西。 你的13个元素使散列键“vsrv”变成14,这是偶数。 Perl将采用任何具有偶数个元素的列表,并且很乐意将它变成一个散列。 如果你的数组有15个元素的哈希键总共有另一个元素,你会得到一个警告: Odd number of elements in hash assignment at foo.pl line 28.

有关更多信息,请参见perldoc perlreftut “制作参考文献”和“使用参考文献”。

How do you store the array in a hash and access it later?

You need to store your array by reference like this:

my %attribs = ( vsrv => \@vsrvAttribs );

Note the backslash before the @ sigil. This tells perl that you want a reference to the array.

Then when access the array stored in $attribs{vsrv} you need to treat it as a reference instead of as an array. You'll do something like this:

foreach my $classTypeKey (keys %classTypes) { # make a copy of the array by dereferencing my @attribs = @{ $attribs{$classTypeKey} }; # OR just use the array reference if profiling shows performance issues: my $attribs = $attribs{$classTypeKey} # these will show the same thing if you haven't done anything to @attribs # in the interim print Dumper(\@attribs); print Dumper($attribs); }

Why did you only get one value and where did the rest of the array go?

Your missing values from @vsrvAttribs weren't lost they were assigned as keys and values to %attribs itself. Try adding the following just after you made your assignment and you'll see it for yourself:

my %attribs = ( vsrv => @vsrvAttribs ); print Dumper(\%attribs);

You'll see output like this:

$VAR1 = { 'vsrv_contact' => 'vsrv_state', 'vsrv_virt_platf' => 'vsrv_owner', 'vsrv' => 'Code', 'vsrv_name' => 'vsrv_vcpu_no', 'vsrv_mgmt_ip' => 'vsrv_os', 'Description' => 'vsrv_id', 'vsrv_vmem_size' => 'vsrv_vdspace_alloc' };

This is because perl interpreted your assignment by expanding the contents @vsrvAttribs as multiple arguments to the list literal ():

my %attribs = ( # your key => first value from array vsrv => 'Code', # subsequent values of the array Description => 'vsrv_id', vsrv_name => 'vsrv_vcpu_no', vsrv_vmem_size => 'vsrv_vdspace_alloc', vsrv_mgmt_ip => 'vsrv_os', vsrv_virt_platf => 'vsrv_owner', vsrv_contact => 'vsrv_state', );

This is legal in perl and there are reasons where you might want to do this but in your case it wasn't what you wanted.

Incidentally, you would have been warned that perl was doing something that you might not want if you had an even number of elements in your array. Your 13 elements plush the hash key "vsrv" makes 14 which is even. Perl will take any list with an even number of elements and happily make it into a hash. If your array had another element for 15 elements total with the hash key you would get a warning: Odd number of elements in hash assignment at foo.pl line 28.

See "Making References" and "Using References" in perldoc perlreftut for more information.

更多推荐

@vsrvAttribs,array,数组,Dumper,电脑培训,计算机培训,IT培训"/> <meta name="

本文发布于:2023-08-03 16:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1394548.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   元素   大小   Referenced   element

发布评论

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

>www.elefans.com

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