Hi All, I need to add the hashes is there any way to add actually my data is in the given format data = [ {"first" => 1 ,"second" => 2}, {"first" => 2 ,"second" => 3}, {"first" => 3 ,"second" => 4} ] I want to acheive it through the loop So, I took 2 hashes as result = Hash.new data = Hash.new for k in 1..4 do data = {"first"=>k , "second"=>k+1} result = result,data end in the above the result variable result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, "second"=>5}]] But my Output should be as result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] I tried with the merge,update but those are updating or the changing the existing the feilds.. the not appending How can I achieve the output if any help you can save my day... Thanks --~--~---------~--~----~------------~-------~--~----~ 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 thnk this url will help u try this http://snippets.dzone.com/posts/show/1535 On Nov 24, 9:49 pm, Hema <hema.soli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > I need to add the hashes is there any way to add > > actually my data is in the given format > data = [ {"first" => 1 ,"second" => 2}, > {"first" => 2 ,"second" => 3}, > {"first" => 3 ,"second" => 4} ] > > I want to acheive it through the loop So, I took 2 hashes as > > result = Hash.new > data = Hash.new > > for k in 1..4 do > data = {"first"=>k , "second"=>k+1} > result = result,data > end > > in the above the result variable > > result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, > "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, > "second"=>5}]] > > But my Output should be as > result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, > {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] > > I tried with the merge,update but those are updating or the changing > the existing the feilds.. the not appending > > How can I achieve the output if any help you can save my day... > Thanks--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hema Gonaboina wrote:> Hi All, > I need to add the hashes is there any way to add > > > actually my data is in the given format > data = [ {"first" => 1 ,"second" => 2}, > {"first" => 2 ,"second" => 3}, > {"first" => 3 ,"second" => 4} ] > > I want to acheive it through the loop So,Perhaps like this : data = [] for k in 1..4 do data[k-1] = {"first"=>k , "second"=>k+1} end HTH -- 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?hl=en -~----------~----~----~----~------~----~------~--~---
* Hema <hema.solivar-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> [2008-11-24 21:49:34 -0800]:> > Hi All, > I need to add the hashes is there any way to add > > > actually my data is in the given format > data = [ {"first" => 1 ,"second" => 2}, > {"first" => 2 ,"second" => 3}, > {"first" => 3 ,"second" => 4} ] > > I want to acheive it through the loop So, I took 2 hashes as > > result = Hash.new > data = Hash.new > > for k in 1..4 do > data = {"first"=>k , "second"=>k+1} > result = result,data > end > > > in the above the result variable > > result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, > "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, > "second"=>5}]] > > But my Output should be as > result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, > {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}](1..4).inject([]){|result,i| result << {"first" => i, "second" => i+1}}> > I tried with the merge,update but those are updating or the changing > the existing the feilds.. the not appending > > How can I achieve the output if any help you can save my day... > Thanks > --~--~---------~--~----~------------~-------~--~----~ > 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@googlegroups.com > For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en > -~----------~----~----~----~------~----~------~--~----- jan=callcc{|jan|jan};jan.call(jan)
Hema, to be honest, I''m not really sure what exactly you want to achieve, but maybe you can try this: result = Array.new for k in 1..4 do result << {"first" => k , "second"=> k + 1} end The result is: [{"second"=>2, "first"=>1}, {"second"=>3, "first"=>2}, {"second"=>4, "first"=>3}, {"second"=>5, "first"=>4}] As far as I know there is no easy way to sort the elements of the inner hashes, but that shouldn''t raise any problem. Let me know it this works for you. On Nov 24, 9:49 pm, Hema <hema.soli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hi All, > I need to add the hashes is there any way to add > > actually my data is in the given format > data = [ {"first" => 1 ,"second" => 2}, > {"first" => 2 ,"second" => 3}, > {"first" => 3 ,"second" => 4} ] > > I want to acheive it through the loop So, I took 2 hashes as > > result = Hash.new > data = Hash.new > > for k in 1..4 do > data = {"first"=>k , "second"=>k+1} > result = result,data > end > > in the above the result variable > > result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, > "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, > "second"=>5}]] > > But my Output should be as > result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, > {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] > > I tried with the merge,update but those are updating or the changing > the existing the feilds.. the not appending > > How can I achieve the output if any help you can save my day... > Thanks--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi , I need to acheive the result to be as result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] since this hash is the PDF::SimpleTable.data format the table data accepts the hash values with the keys as columns and the values as the table rows In this case i have to create the report from the database records I have the Array of employee objects with the fields I need to put them in table format so that first I have to make the column names as the table column headings and then need to form a row of values For example my ''employees'' contains the employee object then, each object has to become one value in the hash data = Hash.new result = Hash.new for employees.each do |record| data = {"Sno" => record.sno, Here i have one record "UserName" => record.username, "designation" => record.designation, "salary" => record.salary } result = data,result end I want result should be in the following format result = [{"Sno" => 1,"UserName" => "ppp","designation" => "se","salary" => 10000},{"Sno" => 2,"UserName" => "xxx","designation" => "se","salary" => 10000},{"Sno" => 3,"UserName" => "yyy","designation" => "se","salary" => 10000},{"Sno" => 4,"UserName" => "zzz","designation" => "se","salary" => 10000}] then I could replace the table data table.data.replace result table.render_on(pdf) But I am not getting the required format? How to achieve that? On Nov 25, 3:30 pm, napster <surinaps...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hema, to be honest, I''m not really sure what exactly you want to > achieve, but maybe you can try this: > > result = Array.new > > for k in 1..4 do > result << {"first" => k , "second"=> k + 1} > end > > The result is: > > [{"second"=>2, "first"=>1}, {"second"=>3, "first"=>2}, {"second"=>4, > "first"=>3}, {"second"=>5, "first"=>4}] > > As far as I know there is no easy way to sort the elements of the > inner hashes, but that shouldn''t raise any problem. > > Let me know it this works for you. > > On Nov 24, 9:49 pm, Hema <hema.soli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi All, > > I need to add the hashes is there any way to add > > > actually my data is in the given format > > data = [ {"first" => 1 ,"second" => 2}, > > {"first" => 2 ,"second" => 3}, > > {"first" => 3 ,"second" => 4} ] > > > I want to acheive it through the loop So, I took 2 hashes as > > > result = Hash.new > > data = Hash.new > > > for k in 1..4 do > > data = {"first"=>k , "second"=>k+1} > > result = result,data > > end > > > in the above the result variable > > > result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, > > "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, > > "second"=>5}]] > > > But my Output should be as > > result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, > > {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] > > > I tried with the merge,update but those are updating or the changing > > the existing the feilds.. the not appending > > > How can I achieve the output if any help you can save my day... > > Thanks--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Hi , I need to acheive the result to be as result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] since this hash is the PDF::SimpleTable.data format the table data accepts the hash values with the keys as columns and the values as the table rows In this case i have to create the report from the database records I have the Array of employee objects with the fields I need to put them in table format so that first I have to make the column names as the table column headings and then need to form a row of values For example my ''employees'' contains the employee object then, each object has to become one value in the hash data = Hash.new result = Hash.new for employees.each do |record| data = {"Sno" => record.sno, Here i have one record "UserName" => record.username, "designation" => record.designation, "salary" => record.salary } result = data,result end I want result should be in the following format result = [{"Sno" => 1,"UserName" => "ppp","designation" => "se","salary" => 10000},{"Sno" => 2,"UserName" => "xxx","designation" => "se","salary" => 10000},{"Sno" => 3,"UserName" => "yyy","designation" => "se","salary" => 10000},{"Sno" => 4,"UserName" => "zzz","designation" => "se","salary" => 10000}] then I could replace the table data table.data.replace result table.render_on(pdf) But I am not getting the required format? How to achieve that? On Nov 25, 2:15 pm, Francois Beuraud <rails-mailing-l...@andreas- s.net> wrote:> Hema Gonaboina wrote: > > Hi All, > > I need to add the hashes is there any way to add > > > actually my data is in the given format > > data = [ {"first" => 1 ,"second" => 2}, > > {"first" => 2 ,"second" => 3}, > > {"first" => 3 ,"second" => 4} ] > > > I want to acheive it through the loop So, > > Perhaps like this : > > data = [] > for k in 1..4 do > data[k-1] = {"first"=>k , "second"=>k+1} > end > > HTH > > -- > Posted viahttp://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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Think in terms of the domain. What you need is an array of hashes. Each hash is a representation of your domain object (employee record) employees.collect {|employee| employee.attributes} Thats it. If there are any particular attributes that you don''t want, e.g. created_at, then use something like employees.collect {|employee| employee.attributes.except(:created_at)} --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
Means I can get the hashes as Result = Hash.new Result = employees.collect {|employee| employee.attributes} then the result contails the array of hashes? On Nov 26, 12:23 pm, Pradeep Gatram <pradeep.gat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Think in terms of the domain. What you need is an array of hashes. > Each hash is a representation of your domain object (employee record) > > employees.collect {|employee| employee.attributes} > > Thats it. > > If there are any particular attributes that you don''t want, e.g. > created_at, then use something like > > employees.collect {|employee| employee.attributes.except(:created_at)}--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s great it worked fine But I have another problem, please check the code Fields = Array.new Fields [0] = "sno" Fields [1] = "username" Fields [2] = "designation" Fields [3] = "salary" Fields [4] = "dob" Fields [5] = "email" Fields [6] = "doj" These are the fields of the object ''Employee'' The Employees array contains of the set of employee objects... I need to have the array of hashes as [{employee_record1}, {employee_record2}...... n records] For this i tried as the... result = [] for employee in Employees res = {} for field in fields res << { field => employee.send(field)} end result << res end But it is not working... Please Help me Thank You On Nov 25, 3:07 pm, Xie Hanjian <jan.h....-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> * Hema <hema.soli...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> [2008-11-24 21:49:34 -0800]: > > > > > > > Hi All, > > I need to add the hashes is there any way to add > > > actually my data is in the given format > > data = [ {"first" => 1 ,"second" => 2}, > > {"first" => 2 ,"second" => 3}, > > {"first" => 3 ,"second" => 4} ] > > > I want to acheive it through the loop So, I took 2 hashes as > > > result = Hash.new > > data = Hash.new > > > for k in 1..4 do > > data = {"first"=>k , "second"=>k+1} > > result = result,data > > end > > > in the above the result variable > > > result = [[[[{}, [{"first"=>1, "second"=>2}]], [{"first"=>2, > > "second"=>3}]], [{"first"=>3, "second"=>4}]], [{"first"=>4, > > "second"=>5}]] > > > But my Output should be as > > result = [{"first"=>1, "second"=>2}, {"first"=>2, "second"=>3}, > > {"first"=>3, "second"=>4}, {"first"=>4, "second"=>5}] > > (1..4).inject([]){|result,i| result << {"first" => i, "second" => i+1}} > > > > > I tried with the merge,update but those are updating or the changing > > the existing the feilds.. the not appending > > > How can I achieve the output if any help you can save my day... > > Thanks > > > > -- > jan=callcc{|jan|jan};jan.call(jan) > > application_pgp-signature_part > < 1KViewDownload--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
That''s great it worked fine But I have another problem, please check the code Fields = Array.new Fields [0] = "sno" Fields [1] = "username" Fields [2] = "designation" Fields [3] = "salary" Fields [4] = "dob" Fields [5] = "email" Fields [6] = "doj" These are the fields of the object ''Employee'' The Employees array contains of the set of employee objects... I need to have the array of hashes as [{employee_record1}, {employee_record2}...... n records] For this i tried as the... result = [] for employee in Employees res = {} for field in fields res << { field => employee.send(field)} end result << res end But it is not working... Please Help me Thank You On Nov 26, 12:23 pm, Pradeep Gatram <pradeep.gat...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Think in terms of the domain. What you need is an array of hashes. > Each hash is a representation of your domain object (employee record) > > employees.collect {|employee| employee.attributes} > > Thats it. > > If there are any particular attributes that you don''t want, e.g. > created_at, then use something like > > employees.collect {|employee| employee.attributes.except(:created_at)}--~--~---------~--~----~------------~-------~--~----~ 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---