Hi, I have two arrays: one that contains ids and data values and one that contains ids and names. There is a relationship between the ids in the two arrays. How do I create a third array that contains names and data values? This is basically a lookup to replace ids pulled from a database with a readable name. Thanks, David -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I assume you mean''t Hashes and not Arrays? This assumes the elements are in the same order: names = {:one => "one", :two => "two"} data = {"one" => "foo", "two" => "bar"} hash = {} names.values.zip(data.values).each {|key, value| hash[key] = value} hash => {"two"=>"bar", "one"=>"foo"} On 22/09/06, David Lelong <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > Hi, > > I have two arrays: one that contains ids and data values and one that > contains ids and names. There is a relationship between the ids in the > two arrays. How do I create a third array that contains names and data > values? This is basically a lookup to replace ids pulled from a > database with a readable name. > > Thanks, > > David > > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I''m using find to return to sets of data from a database that need to be combined to display in an html table. The id is in one array, but the corresponding name I need to display is in the other array because it''s in another table. How do I combine them so I can display the name in the table instead of the id? Or is it better to do a join using rails so I only return a single set of data from the database? Thanks, David -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
Array#zip Or, a join. On 9/22/06, David Lelong <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > I''m using find to return to sets of data from a database that need to be > combined to display in an html table. The id is in one array, but the > corresponding name I need to display is in the other array because it''s > in another table. How do I combine them so I can display the name in > the table instead of the id? > > Or is it better to do a join using rails so I only return a single set > of data from the database? > > Thanks, > > David > > > -- > Posted via http://www.ruby-forum.com/. > > > >-- When a man says he approves of something in principle, it means he hasn''t the slightest intention of putting it into practice. -- Bismarck --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---
I''d go with a JOIN, it''s no doubt faster. On 23/09/06, Michael Campbell <michael.campbell-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > Array#zip > > Or, a join. > > On 9/22/06, David Lelong <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > > I''m using find to return to sets of data from a database that need to be > > combined to display in an html table. The id is in one array, but the > > corresponding name I need to display is in the other array because it''s > > in another table. How do I combine them so I can display the name in > > the table instead of the id? > > > > Or is it better to do a join using rails so I only return a single set > > of data from the database? > > > > Thanks, > > > > David > > > > > > -- > > Posted via http://www.ruby-forum.com/. > > > > > > > > > > -- > When a man says he approves of something in principle, it means he > hasn''t the slightest intention of putting it into practice. -- > Bismarck > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk -~----------~----~----~----~------~----~------~--~---