how to convert a hash into an array with particular order? (ruby) -


I have a hash of hash (@post) and I want to save it to CSV. The strange thing for me is that my approach works for header, but not for rows if I

CSV < & Lt; (@ Post_csv_order.each {| element | puts single_post [element]})

I can see the correct string on the screen, but the CSV file does not have the value of the hash key that has the value Price Basically, I have 5 rows of headers in my CSV instead of the right values.

I think I must convert the post (key) hash based on @ post_csv_order with an order based on post @ [post]. @Post [Key] hash is a hash method but it will not give me the order I need.

I think that you can suggest a different approach and can not use the hash of the hand, but I would also like to know how to convert hash into an array in which something The pre-defined sequence is

On the other hand if there is an easy way to save CSV / a hash of hash in the table , then I would love to re-write my code Is happening

'code' is required 'fast_csv' requirement 'PP' @post = {123 = & gt; {"Forum ID" = & gt; "123", "Post Author" = & gt; "Thom"}, 222 = & gt; {"Forum ID" = & gt; "222", "Post Author" = & gt; "Mark"}, 345 = & gt; {"Forum ID" = & gt; "345", "Post Author" = & gt; "Heel"}, 444 = & gt; {"Forum ID" = & gt; "444", "Post Author" = & gt; "Eli"}} @post_cod_order- ["forummed", "post author"] forres csv Open ("test.csv", "w", {: quote_char = & gt; '',: col_sep = & gt; ',',:;); CSV | CSV & lt; & Lt; (@ Post_csv_order.each {| element | element] element:}) @ Post.each_value do | Single_post | Csv & lt; & Lt; (@ Post_csv_order.each {| element | single_post [element]}) #ccv & lt; & Lt; (@ Post_csv_order.each {| element | puts single_post [element]}) End of End

< P> This row:

  csv & lt; & Lt; @ Post_csv_order.each {| Element | On single_post [element]}  

@post_csv_order array and return the array, which is not what you want. Each and blocks do not really do anything in this context. If you change every to De> map this will work as you wish:

  csv < & Lt; @ Post_csv_order.map {| Element | |  

block

/ P>


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 -