Felipe Pieretti Umpierre wrote in post #1046904:
Hello, what I want to do, the user gonna insert a string with right

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@first.com,My Name"; "myemail@second.com,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

# Start by splitting the string at the semicolons:
arr1 = params[ :email ][ :email ].split( ";" )

# Result:
[ "myemail@first.com,My Name", "myemail@second.com,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
h[arr2[0]] = arr2[1]

# Append the hash to the final array
emails << h

# Final result:
[ { "myemail@first.com": "My Name" }, { "myemail@second.com", "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@googlegroups.com.
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.

Search Discussions

Discussion Posts


Related Discussions

Discussion Navigation
viewthread | post
posts ‹ prev | 5 of 5 | next ›
Discussion Overview
grouprubyonrails-talk @
postedFeb 15, '12 at 1:58a
activeFeb 15, '12 at 3:03p

1 user in discussion

Robert Walker: 5 posts



site design / logo © 2021 Grokbase