RE: Unknown level of hash

[email protected] ("Zhuang Li")
Newsgroups perl.perl6.language,perl.fwp
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
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.