ruby - Creating multi-dimensional array of hashes -
I want to loop through a multi-dimensional array:
array = [[1 , 2,3,4,5], [6,7,8,9,10]]
and create a hash with keys from any other array:
< I have the following code to do so:
>
hash = hash.new multirere = arrays.Anupate array.each do | Value | Key Values.Each | Key | I = Key value Index (key) hash [key] = value [i] at the end hash multiraure and lieutenant; & Lt; Hash ultimately puts multi-dimensional
puts hash
output:
{"one" => 1, "two" = & gt; 2, "three" => 3, "four" = & gt; 4, "five" = & gt; 5} {"A" = & gt; 6, "two" = & gt; 7, "three" = & gt; 8, "four" = & gt; 9, "five" = & gt; 10}
and the last is multiple
:
{"one" => 6, "two" = & gt; 7, "three" = & gt; 8, "four" = & gt; 9, "five" => 10} {"one" = & gt; 6, "two" = & gt; 7, "three" = & gt; 8, "four" = & gt; 9, "five" = & gt; 10}
I can not do why I do not know:
{"one" => 1, "two" = & gt; 2, "three" => 3, "four" = & gt; 4, "five" = & gt; 5}
for the final multiArray
.
There are two entries for the same hash in your array, so if you change the hash object anywhere , Then it will change in both array entries. To avoid having the same exact hash object in each array entry, you can use the multiArray & lt; & Lt; By replacing, you can duplicate the hash before inserting. Hash
to multicolored
& Lt; Hash.dup
Comments
Post a Comment