RE: Unknown level of hash
"Zhuang Li" <[email protected]>
| Newsgroups | gmane.comp.lang.perl.fun,gmane.comp.lang.perl.perl6.language |
|---|---|
| Message-ID | <7A7034183C845E4B9963F65505BA34D12193A0@vchqex01.wl.corp.valueclick.com> |
> -----Original Message----- > From: Luke Palmer [mailto:[email protected]] > Sent: Tuesday, March 29, 2005 5:43 AM > To: Zhuang Li > Cc: Jeff Yoak; [email protected]; [email protected] > Subject: Re: Unknown level of hash > > Zhuang Li writes: > > Yes. I think it's both useful and fun. I was thinking something similar > > to > > @hash{@a} = map{1} @a; > > > > But getting "$hash->{E1}->{E2}->...->{En} = 1;" instead of "$hash{E1} = > > 1; ... $hash{En} =1;". > > Yeah, like this: > > %hash{dims @a} = (1) xx Inf; > > > What I'd really like to do is: > > > > Given @a = ('E1', 'E2', ..., 'En'); > > @b = ('K1', 'K2', ..., 'Km'); > > @c = ('V1', 'V2', ..., 'Vm'); > > > > To get the following in one line: > > $hash->{E1}->...->{En}->{K1} = 'V1'; > > $hash->{E1}->...->{En}->{K2} = 'V2'; > > .... > > $hash->{E1}->...->{En}->{Km} = 'Vm'; > > %hash{dims @a}{@b} = @c; That's exactly the type of thing I expected to have. > > Were you asking the right thing? > > Luke My real world problem is: Given: $a = [ ['E11', 'E12', ..., 'E1n'], ... ['Em1', 'Em2', ..., 'Emn'], ]; And an array @b which contains indexes for each row in $a, for example: @b = (2, 0, 3); # length of the array could change. Consider this to be the key fields to table $a To get: # for the first row: $hash->{E13}->{E11}->{E14}->{E11} = 'E11'; ... # for each element in first row $hash->{E13}->{E11}->{E14}->{E1n} = 'E1n'; # for the last element in row1 ... # similar thing for each row # for the last row: $hash->{Em3}->{Em1}->{Em4}->{Em1} = 'Em1'; ... $hash->{Em3}->{Em1}->{Em4}->{Emn} = 'Emn'; john