Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.ruby > #3424 > unrolled thread
| Started by | Sira PS <ploy.sukachai@gmail.com> |
|---|---|
| First post | 2011-04-24 03:02 -0500 |
| Last post | 2011-04-24 11:01 -0500 |
| Articles | 5 — 4 participants |
Back to article view | Back to comp.lang.ruby
Can I split the array from text? Sira PS <ploy.sukachai@gmail.com> - 2011-04-24 03:02 -0500
Re: Can I split the array from text? Joey Zhou <yimutang@gmail.com> - 2011-04-24 08:21 -0500
Re: Can I split the array from text? Joey Zhou <yimutang@gmail.com> - 2011-04-24 08:35 -0500
Re: Can I split the array from text? Roger Braun <roger@rogerbraun.net> - 2011-04-24 09:06 -0500
Re: Can I split the array from text? Brian Candler <b.candler@pobox.com> - 2011-04-24 11:01 -0500
| From | Sira PS <ploy.sukachai@gmail.com> |
|---|---|
| Date | 2011-04-24 03:02 -0500 |
| Subject | Can I split the array from text? |
| Message-ID | <f49ba0d3c9809988b28fc4042f50998a@ruby-forum.com> |
If I have an array like this ["Member", "Friends", "Hello", "Components", "Family", "Lastname"] And I need to split this array from "Components" and get 2 arrays which are ["Member", "Friends", "Hello"] and ["Family", "Lastname"] Can I do that and how? -- Posted via http://www.ruby-forum.com/.
[toc] | [next] | [standalone]
| From | Joey Zhou <yimutang@gmail.com> |
|---|---|
| Date | 2011-04-24 08:21 -0500 |
| Message-ID | <6d8f6a42e814dec670357ff39a49eee6@ruby-forum.com> |
| In reply to | #3424 |
Maybe you can refer to Enumerable#slice_before
Here's a sample code:
a = ["Member","Friends","Hello","Components","Family","Lastname"]
b = a.slice_before {|elem| elem == "Components" }.to_a
p b[0] #=> ["Member", "Friends", "Hello"]
p b[1] #=> ["Components", "Family", "Lastname"]
Joey
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Joey Zhou <yimutang@gmail.com> |
|---|---|
| Date | 2011-04-24 08:35 -0500 |
| Message-ID | <f62f85247ec2e1df9d06ddccb69646a9@ruby-forum.com> |
| In reply to | #3424 |
Here's a tricky one:)
a = ["Member","Friends","Hello","Components","Family","Lastname"]
b = a.partition {|elem| true unless elem=="Components"..false }
p b
#=> [["Member","Friends","Hello"], ["Components","Family",
"Lastname"]]
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [next] | [standalone]
| From | Roger Braun <roger@rogerbraun.net> |
|---|---|
| Date | 2011-04-24 09:06 -0500 |
| Message-ID | <BANLkTin3TKBv-3BAs=8FksdRUNeUx2BjcQ@mail.gmail.com> |
| In reply to | #3434 |
How about this?
module Enumerable
def split(sep = nil)
res = Array.new
part = Array.new
self.each do |el|
if block_given? ? yield(el) : el === sep then
res.push(part)
part = Array.new
else
part.push(el)
end
end
res.push(part)
res
end
end
ruby-1.9.2-p180 :015 > ar = ["Member", "Friends", "Hello",
"Components", "Family", "Lastname"]
=> ["Member", "Friends", "Hello", "Components", "Family", "Lastname"]
ruby-1.9.2-p180 :016 > ar.split("Friends")
=> [["Member"], ["Hello", "Components", "Family", "Lastname"]]
ruby-1.9.2-p180 :017 > ar.split do |el| el[-1] == "s" end
=> [["Member"], ["Hello"], ["Family", "Lastname"]]
--
Roger Braun
rbraun.net | humoralpathologie.de
[toc] | [prev] | [next] | [standalone]
| From | Brian Candler <b.candler@pobox.com> |
|---|---|
| Date | 2011-04-24 11:01 -0500 |
| Message-ID | <9d7348e487437e591621461c32a68b1e@ruby-forum.com> |
| In reply to | #3424 |
Sira PS wrote in post #994712:
> If I have an array like this
>
> ["Member", "Friends", "Hello", "Components", "Family", "Lastname"]
>
> And I need to split this array from "Components" and get 2 arrays which
> are
>
> ["Member", "Friends", "Hello"]
>
> and
>
> ["Family", "Lastname"]
>
> Can I do that and how?
I think this is the simplest way:
a = ["Member", "Friends", "Hello", "Components", "Family", "Lastname"]
i = a.index("Components")
p a[0...i]
p a[i+1..-1]
--
Posted via http://www.ruby-forum.com/.
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.ruby
csiph-web