hash assignment when (key => value) are stored in an array? (ruby) -


I have hash (@post) hash where I want to keep the order of hash I want to place the key (@ post_csv_order) and the relationship key => value in the array .

I know that the final number of both elements are @ost hashes and key => values ​​in the array.

I do not know how to specify a hash in a loop, for all the elements in the array one by one @ post_csv_order [0] [0] = & gt; @post_csv_order [0] [1] works well

  # requires 'pp' @ post = {} forum_id = 123 # sample value only .. To work this sample script, post_title = "test post" @post_csv_order = [["forum id", forum_id], ["post title", post_title]] if @post [forum_id] == neil @ post [ Forum_id] = {@post_csv_order [0] [0] = & gt; @post_csv_order [0] [1], @post_com_order [1] [0] = & gt; @post_csv_order [1] [1] #@post_csv_order.map {| Element | Element [0] = & gt; Element [1]} #@post_csv_order.each_index {| Index | @post_csv_order [index] [0] = & gt; @ Post_csv_order [index] [1]}} End PP @ Post  

The desired hash assignment should be like that

{123 = & gt; ; {"Post Title" = & gt; "Test Post", "FourPrice" => 1) Itemprop = "text">

I'm not sure that I fully understand your question but I think you want to change a 2d array in a hash Are there.

It looks like you have an array:

  array = [[:: एफु, 1], [: bar, 2], [: falcon, 3] ]  

You can create a hash:

  hash = array-indject ({}) {| H, e H [e [0]] = e [1]; H} # = & gt; {: Foo = & gt; 1 ,: bar = & gt; 2 ,: baz = & gt; 3}  

and you can recover the key in the correct order with the following:

  key = array.inject ([]) {| A, e A & lt; & Lt; E [0]} = & gt; [: Foo ,: bar ,: baz]  

Were you searching for it?


Comments

Popular posts from this blog

oracle - The fastest way to check if some records in a database table? -

php - multilevel menu with multilevel array -

jQuery UI: Datepicker month format -