Hello, I was testing a only string, and split it to get a array, I make a .each, but when I put to insert, it just insert the last index. emails_controller.rb emails = params[ :email ][ :email ].split( ";" ) @e = emails action <%= @e %> return : ["myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org,My Name", "myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org,My Name"] emails_controller.rb emails.each do |e| for_each_one = e.split( "," ) @a = for_each_one #@email.name = for_each_one[ 0 ] #@email.email = for_each_one[ 1 ] #@email.save # #if @email.valid? # @group.emails << @email #end end action <% @a.each do |a| %> <%= a %><br /> <% end %> return : myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org My Name Thank you. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Felipe Pieretti Umpierre wrote in post #1046832:> Hello, I was testing a only string, and split it to get a array, I make > a .each, but when I put to insert, it just insert the last index.As far as I can tell it''s doing exactly what you asked it to do: Let me explain....> emails_controller.rb > > emails = params[ :email ][ :email ].split( ";" ) > @e = emails > > action > <%= @e %> > > return : ["myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org,My Name", "myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org,My Name"]Your array with two elements...> emails_controller.rb >Enumerate each of the two elements...> emails.each do |e| > for_each_one = e.split( "," ) > > @a = for_each_one1. First time through set @a to a new array containing the split of the first element 2. Second time through replace @a with new array containing the split of the second element> > #@email.name = for_each_one[ 0 ] > #@email.email = for_each_one[ 1 ] > #@email.save > # > #if @email.valid? > # @group.emails << @email > #end > > end > > actionEnumerate the two elements contained in @a (i.e. [ ''myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org'', ''My Name'' ])> <% @a.each do |a| %> > <%= a %><br /> > <% end %> > > return : > > myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org > My NameEnd of line.... -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Felipe Pieretti Umpierre wrote in post #1046832: Maybe you wanted....> emails_controller.rb@a = []> emails.each do |e| > > for_each_one = e.split( "," )@a << for_each_one Append split array to @a array. (i.e. an array of arrays).> > #@email.name = for_each_one[ 0 ] > #@email.email = for_each_one[ 1 ] > #@email.save > # > #if @email.valid? > # @group.emails << @email > #end > > end > > action > > <% @a.each do |a| %> > <%= a[0], a[1] %><br />The .each here would yield the two arrays contained within the outer array.> <% end %> > > return : > > myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org > My Name > > Thank you.I''m not sure if that''s exactly what you wanted, but should point you in the right direction. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Hello, what I want to do, the user gonna insert a string with right combinations. So, the user gonna put, the email[comma]name[semincolon] This way, I need to split the semicolon, to have the array of strings, after I need to split the comma, to have the name in a index, and the email in another. emails = params[ :email ][ :email ].split( ";" ) array = [] emails.each do |e| foreach = e.split( ";" ) array << foreach @a = array end ["myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org,My Name"] ["myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org,My Name 2"] But I can''t access the index to split the comma. Thank you -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.
Felipe Pieretti Umpierre wrote in post #1046904:> Hello, what I want to do, the user gonna insert a string with right > combinations. > > So, the user gonna put, the email[comma]name[semincolon] > > This way, I need to split the semicolon, to have the array of strings, > after I need to split the comma, to have the name in a index, and the > email in another.Okay, I thought my earlier explanation would be sufficient to help you achieve your goal, but I''ll try to explain further: You are starting with this: "myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org,My Name"; "myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org,My Name" I can''t guess what you want in your final result. There are multiple ways to split and store this. So I''ll explain what I would do in this case, assuming I want to keep all the information in the original string: # Start by splitting the string at the semicolons: arr1 = params[ :email ][ :email ].split( ";" ) # Result: [ "myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org,My Name", "myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org,My Name" ] # Declare an array to store the final data emails = [] # Enumerate the array we split arr1.each do |str| # For each string in arr1 split at the comma arr2 = str.split(",") # Put the address and name in a hash (keyed by email address in this case) h[arr2[0]] = arr2[1] # Append the hash to the final array emails << h end # Final result: [ { "myemail-S8S1utOw9MsAvxtiuMwx3w@public.gmane.org": "My Name" }, { "myemail-4cajJJ5tN1zQT0dZR+AlfA@public.gmane.org", "My Name" } ] There might be some shortcut to the above procedure, but I wanted to show all the individual steps to be clear what''s happening. As I said before, this is only one possible way to store the results keeping all the original data. You''ll have to decide what is best for your particular case. -- 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-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFF+G/Ez6ZCGd0@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en.