hopefully someone might be able to shed some light on this. I have a ruby test application which I''m fetching data from a database. I want to store the results into a hash based on the following principle; loop through the rows does the hash contain a key value as read from that row if it does then place the row into an array and then add it to an array of rows which might already be within the hash if the hash doesn''t contain the key then create a key and add the row to an array an append it to the hash unfortunately i''m relatively new to Ruby and can''t figure out how to do this. In a few simply tests I declare a variable called a = [] when I loop over the returned database rows using row, I assign row into a a += row this works but rather than the array being a length of the total number of rows, it''s actually the length of all the rows multiplied by the number of columns. I guess the first problem is getting the rows into a correct array. Once this is done I should be able to say "hash, have you got a key with the value 1" - the hash might say "yes, and here is an array of rows." I''ll take that array of rows and add another row to it and save back to the hash. can anyone help? --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On 15 Jan 2008, at 12:17, hiddenhippo wrote:> > hopefully someone might be able to shed some light on this. > > I have a ruby test application which I''m fetching data from a > database. I want to store the results into a hash based on the > following principle; > > loop through the rows > does the hash contain a key value as read from that row > if it does then place the row into an array and then add it to an > array of rows which might already be within the hash > if the hash doesn''t contain the key then create a key and add the > row to an array an append it to the hash > > > unfortunately i''m relatively new to Ruby and can''t figure out how to > do this. In a few simply tests I declare a variable called > > a = [] > > when I loop over the returned database rows using row, I assign row > into a > > a += rowSome thing like result = {} rows.each do |row| key = find_key_for_row row result[key] ||= [] result[key] << row end maybe?> > > this works but rather than the array being a length of the total > number of rows, it''s actually the length of all the rows multiplied by > the number of columns. I guess the first problem is getting the rows > into a correct array. Once this is done I should be able to say > > "hash, have you got a key with the value 1" - the hash might say "yes, > and here is an array of rows." I''ll take that array of rows and add > another row to it and save back to the hash. > > > can anyone help? > > > >--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 15, 2008, at 6:17 AM, hiddenhippo wrote:> > can anyone help? >My advice would be to start here: http://www.ruby-doc.org/core/ and look at the Array and Hash classes. You''ll learn a lot and be very happy to did. Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
I made slight progress with feedback from both Phillip and Frederick. I can now assign a key to the hash and query whats in the hash based on that key, but again the hash isn''t storing an array of rows, but rather an array of all the column values from every row... just need to sort this out.... thanks On Jan 15, 12:28 pm, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 15, 2008, at 6:17 AM, hiddenhippo wrote: > > > > > can anyone help? > > My advice would be to start here: > > http://www.ruby-doc.org/core/ > > and look at the Array and Hash classes. You''ll learn a lot and be > very happy to did. > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 15, 2008, at 6:50 AM, hiddenhippo wrote:> > I made slight progress with feedback from both Phillip and Frederick. > > I can now assign a key to the hash and query whats in the hash based > on that key, but again the hash isn''t storing an array of rows, but > rather an array of all the column values from every row... just need > to sort this out.... > > thanksPost the code that you currently have. We''ll see if we can sort it out. Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
Thanks. I''ve pasted the code below, which might I add is just a load of test code. I make the connection to the postgress db and select all the data from a table called test. I then loop on the results, and at the moment I simply dump them into the hash. I plan to add hash.has_key to check beforehand, but the code below doesn''t reflect that. db = PGconn.connect(*****) #db.type_translation = true res = db.exec('' select * from test'') a = {} res.each do |r| start_date = DateTime.parse(r[2]) frequency = r[5].to_i case r[4].to_i when 1 #yearly if !do_yearly?(start_date, frequency) a[r[0]] = r end when 2 #monthly if !do_monthly?(start_date, frequency) a[r[0]] = r end when 3 #weekly if !do_weekly?(start_date, frequency) a[r[0]] = r end end end db.close On Jan 15, 2:08 pm, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 15, 2008, at 6:50 AM, hiddenhippo wrote: > > > > > I made slight progress with feedback from both Phillip and Frederick. > > > I can now assign a key to the hash and query whats in the hash based > > on that key, but again the hash isn''t storing an array of rows, but > > rather an array of all the column values from every row... just need > > to sort this out.... > > > thanks > > Post the code that you currently have. We''ll see if we can sort it out. > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
I''d try a couple of things. see below On Jan 15, 2008, at 9:15 AM, hiddenhippo wrote:> > a = {} > res.each do |r| > start_date = DateTime.parse(r[2]) > frequency = r[5].to_i > case r[4].to_i > when 1 #yearly > if !do_yearly?(start_date, frequency) > a[r[0]] = ra[r[0]] << r instead of a[r[0]] = r The other thing would be to create a[r[0]] as an array explicitly when you check to see if it''s there a[r[0]] = Array.new if !a.has_key?(r[0]) But if Fred offers something, take his advice first :) Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
found a little more information a = Hash.new{|hash,key| hash[key] =[]} and then use a[r[0]] << r if !do_yearly?(start_date, frequency) Do you know Fred then? On Jan 15, 3:29 pm, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d try a couple of things. > > see below > > On Jan 15, 2008, at 9:15 AM, hiddenhippo wrote: > > > > > a = {} > > res.each do |r| > > start_date = DateTime.parse(r[2]) > > frequency = r[5].to_i > > case r[4].to_i > > when 1 #yearly > > if !do_yearly?(start_date, frequency) > > a[r[0]] = r > > a[r[0]] << r > > instead of > > a[r[0]] = r > > The other thing would be to create a[r[0]] as an array explicitly > when you check to see if it''s there > > a[r[0]] = Array.new if !a.has_key?(r[0]) > > But if Fred offers something, take his advice first :) > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
On Jan 15, 2008, at 9:34 AM, hiddenhippo wrote:> > Do you know Fred then? >Only what I see of him in this group. I haven''t seen bad advice come from him yet. And the knowledge he possesses exceeds mine. By quite a bit, it seems. As does that of many others... Peace, Phillip --~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---
oh right, well that might well be the case but at the very least your knowledge exceeds mine. I''m in the process of jumping ships from all out MS to more open source. Played about with Django for a while but got annoyed with the limitations imposed by the template language. Then Ruby on Rails came to light. Done a few small projects on it and feel that I''m coming along with leaps and bounds, I just think the real bottleneck, for me, is learning the Ruby language...not to mention prototype which is somewhat different than JQuery, which I''m pretty proficient in. On Jan 15, 4:21 pm, Phillip Koebbe <phillipkoe...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> On Jan 15, 2008, at 9:34 AM, hiddenhippo wrote: > > > > > Do you know Fred then? > > Only what I see of him in this group. I haven''t seen bad advice come > from him yet. And the knowledge he possesses exceeds mine. By quite > a bit, it seems. As does that of many others... > > Peace, > Phillip--~--~---------~--~----~------------~-------~--~----~ 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?hl=en -~----------~----~----~----~------~----~------~--~---