I have an array of hashes, many of which have shared keys.
I would like to convert this into a matrix for analysis in [R], such that each row represents a hash, and each unique key is a column, which is (blank) or '.' or 'NA' if the hash does not contain that particular key.
Currently I'm planning to find each unique key in the array of hashes, and construct my matrix by looping through each of these for each hash... but there must be a better way??
Thanks!
Example:
my %hash_A = (
A=> 12,
B=> 23,
C=> 'a string'
);
my %hash_B = (
B=> 23,
C=> 'a different string',
D=> 99
);
To give:
A,B,C,D
12,23,'a string',NA
NA, 23, 'a different string', 99
This should convert an array of hashes into a 2D array (
@output1).All output cells where there was no corresponding input value will be populated with
'NA'. (If you don't mind unmapped cells being mapped toundef, then this can be done more concisely — see@output2.)The array
@keyswill say which hash key relates each index position in the output rows.