Parse array of arrays in perl -


i trying parse array of arrays, error

reference found even-sized list expected 

here program:

use modern::perl;  @info=(       ['k1','v1',1,2],       ['k2','v2',2,3]     );  %names=getnames(\@info);  sub getnames {     ($info) = @_;     %names;     foreach $item (@$info) {        $names{@$item[0]}=@$item[1];     }     return \%names; } 

as ivan pointed out, return value not match you're assigning to:

my %names = getnames(\@info);  sub getnames {     ...     return \%names; } 

%names excepts number of elements because it's hash, you're assigning reference hash, single element. hence error.

this line of subroutine little suspect: $names{@$item[0]}=@$item[1];. perhaps meant use $names{$item->[0]}=$item->[1];?

if you're trying translate array of arrays hash keys pointing @ remaining values, can use following:

my @info=(       ['k1','v1',1,2],       ['k2','v2',2,3],     );  %names = map {$_->[0] => [@{$_}[1..$#$_]]} @info;  use data::dump; dd \%names; 

outputs:

{ k1 => ["v1", 1, 2], k2 => ["v2", 2, 3] } 

if however, you're wanting first "value", following sufficient:

my %names = map {$_->[0] => $_->[1]} @info; 

Comments

Popular posts from this blog

android - Get AccessToken using signpost OAuth without opening a browser (Two legged Oauth) -

org.mockito.exceptions.misusing.InvalidUseOfMatchersException: mockito -

google shop client API returns 400 bad request error while adding an item -