perl - Referenced array dropped in size to one element -
Dear partner perl programmer,
I wanted to access 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); via a variable made of variable and a string suffix, which certainly leads to error messages like this
not using the string ("@vsrvAttribs") as an ARRE ref, while "hard riff" in use on cmdbuild.pl line 262. So I decided to get the reference of the array through a hash
my% attribs = (vsrv = & gt; @vsrv attribus) ; And this is the code where I need to get the contents of the above array
foreach my $ classTypeKey (key% classTypes) {my @attribs = $ Properties {$ classTypeKey}; Print dumper (\ @ atribus); } It seems that I can get the reference of array @vsrvAttribs, but when I check the contents of the array with the dump, the array has only got one element < / p>
$ VAR1 = ['code']; Do you have any ideas where the problem can be?
How do you store an array in a hash and access it later?
You need to store your array from this context:
my% attribs = (vsrv = & gt; \ @ vsrv attribus); Note the backslash before @ it tells perl that you want references to the array. Then when accessing the array stored in $ attribs {vsrv} , you need to convert it to the reference array. You will do something like this: foreach Make a copy of the array by removing $ my $ classTypeKey (key% classTypes) {# my @attribs = @ {$ attribs {$ classTypeKey}}; Use the # or array reference if profiling shows performance issues: My $ attribs = $ attribs {$ classTypeKey} # These will show the same thing if you did nothing for @attribs in the interim print dumper (@ @ atribs) is ); Print dumper ($ attribus); } Why did you get only one value and where the remaining array went? Your missing value from
@vsrvAttribs was assigned to them as key and value as % attribs after your assignment Just try adding the following and you will see it for yourself: my% attribs = (vsrv = & gt; @vsrvAttribs); Print dumper (\% attribs); You will see the output like this:
$ VAR1 = {'vsrv_contact' = & gt; 'Vsrv_state', 'vsrv_virt_platf' = & gt; 'Vsrv_owner', 'vsrv' = & gt; 'Code', 'vsrv_name' = & gt; 'Vsrv_vcpu_no', 'vsrv_mgmt_ip' = & gt; 'Vsrv_os', 'Description' = & gt; 'vsrv_id', 'vsrv_vmem_size' = & gt; 'Vsrv_vdspace_alloc'}; The reason for this is that Peral interpreted his assignment as a lot of logic in the list as the extension of the contents of @vsrvAttribs value () : my% attribs = (# your key = & gt; array vsrv = & gt; value before the 'code', the # 'vsrv_id', vsrv_name = & gt; 'vsrv_vcpu_no', vsrv_vmem_size = & gt; 'vsrv_vdspace_alloc', vsrv_mgmt_ip = & gt; 'vsrv_os', vsrv_virt_platf = & gt; 'vsrv_owner', vsrv_contact = & gt; vsrv_state ',); This is legal in Perl and there are reasons where you can do this, but in your case it was not what you wanted.
Incidentally, you have been warned that Pearl was doing something that you do not want if you have too many elements in your array, makes your 13 elements a "vs" hash key Which also makes 14. Pearl will take any list with some elements and will happily make it into a hash If you had another element for 15 elements with a hash key in your array, you get a warning: foo. An odd number of elements in the hash assignment on line 28. "For more information" References "and" Using references ".
Comments
Post a Comment