digitalmars.D.learn - Build Hash from Array in one statement ?
- Gordon (18/18) Feb 22 2014 Hello,
- Tobias Pankrath (4/9) Feb 22 2014 std.array.assocArray.
- =?UTF-8?B?QWxpIMOHZWhyZWxp?= (10/11) Feb 22 2014 If I am allowed to misunderstand :) it as a *single* hash from all of
Hello,
Is there a way to quickly build a hash from the values of an
array?
A one-statement equivalent of:
T[] array_values ;
int[T] hash_values;
hash_values = map!(XXXX) (array_values) ;
Instead of:
for (v; array_values) {
hash_values[v] = 1;
}
in Perl, that would be:
%hash = map { $_ => 1 } array;
---
Or, put differently, is there a way to convert values in an array
to keys of hash in one statement ?
Thanks!
-gordon
Feb 22 2014
--- Or, put differently, is there a way to convert values in an array to keys of hash in one statement ? Thanks! -gordonstd.array.assocArray. -- T[] keys; auto aa = zip(keys, repeat(true)).assocArray;
Feb 22 2014
On 02/22/2014 11:26 AM, Gordon wrote:Is there a way to quickly build a hash from the values of an array?If I am allowed to misunderstand :) it as a *single* hash from all of the values, then you can use D's internal hash algorithm for arrays: import std.stdio; void main() { auto arr = [ 1, 2, 3 ]; writeln(typeid(arr).getHash(&arr)); } Ali
Feb 22 2014









"Tobias Pankrath" <tobias pankrath.net> 