Hello all,
I have a string “apple-orange-grape”. I need to create a array that will
look like
[“apple-orange-grape”,“orange-grape”,“grape”]
thanks
Hello all,
I have a string “apple-orange-grape”. I need to create a array that will
look like
[“apple-orange-grape”,“orange-grape”,“grape”]
thanks
Good Morning,
On Wed, Sep 16, 2009 at 9:01 AM, Re BR [email protected] wrote:
Hello all,
I have a string “apple-orange-grape”. I need to create a array that will
look like
[“apple-orange-grape”,“orange-grape”,“grape”]
x = “apple-orange-grape”
tmp = x.split(‘-’)
(0…tmp.length-1).map{ |i| e[i…-1].join(‘-’) }
John
On Wed, Sep 16, 2009 at 9:20 AM, John W Higgins [email protected]
wrote:
x = “apple-orange-grape”
tmp = x.split(‘-’)
(0…tmp.length-1).map{ |i| e[i…-1].join(‘-’) }
Sorry that should be
(0…tmp.length-1).map{ |i| tmp[i…-1].join(‘-’) }
John
Re BR wrote:
Hello all,
I have a string “apple-orange-grape”. I need to create a array that will
look like
[“apple-orange-grape”,“orange-grape”,“grape”]thanks
str = “apple-orange-grape”
results = []
results << str
pos = 0
while pos = str.index("-", pos)
pos += 1
results << str[pos…-1]
end
p results
–output:–
[“apple-orange-grape”, “orange-grape”, “grape”]
str = “apple-orange-grape”
results = [str]
str.scan(/-/) do |match|
results << $’
end
p results
–output:–
[“apple-orange-grape”, “orange-grape”, “grape”]
Uhg. Are there alternate names for the perl $ variables? I thought
there were, but I can’t find them anywhere.
John W Higgins wrote:
On Wed, Sep 16, 2009 at 9:20 AM, John W Higgins [email protected]
wrote:x = “apple-orange-grape”
tmp = x.split(‘-’)
(0…tmp.length-1).map{ |i| e[i…-1].join(‘-’) }Sorry that should be
(0…tmp.length-1).map{ |i| tmp[i…-1].join(‘-’) }
John
trying to make this as a one liner
a.split(‘.’).map{ |i| a[i…-1].join(‘.’) }
but I am missing something
Re BR,
I noticed in your original statement of the problem you said the input
string was “apple-orange-grape” – so why are you splitting on ‘.’ in
your sample solution?
That would be appropriate for, oh, I don’t know, parts of a domain name,
but not for “apple-orange-grape”
Re BR wrote:
John W Higgins wrote:
On Wed, Sep 16, 2009 at 9:20 AM, John W Higgins [email protected]
wrote:x = “apple-orange-grape”
tmp = x.split(‘-’)
(0…tmp.length-1).map{ |i| e[i…-1].join(‘-’) }Sorry that should be
(0…tmp.length-1).map{ |i| tmp[i…-1].join(‘-’) }
John
trying to make this as a one liner
a.split(‘.’).map{ |i| a[i…-1].join(‘.’) }
but I am missing something
[email protected] Norman wrote:
Re BR,
I noticed in your original statement of the problem you said the input
string was “apple-orange-grape” – so why are you splitting on ‘.’ in
your sample solution?That would be appropriate for, oh, I don’t know, parts of a domain name,
but not for “apple-orange-grape”Re BR wrote:
John W Higgins wrote:
On Wed, Sep 16, 2009 at 9:20 AM, John W Higgins [email protected]
wrote:x = “apple-orange-grape”
tmp = x.split(‘-’)
(0…tmp.length-1).map{ |i| e[i…-1].join(‘-’) }Sorry that should be
(0…tmp.length-1).map{ |i| tmp[i…-1].join(‘-’) }
John
trying to make this as a one liner
a.split(‘.’).map{ |i| a[i…-1].join(‘.’) }
but I am missing something
Hi John,
I pasted the wrong line I meant to say
a.split(‘-’).map{ |i| a[i…-1].join(‘-’) }
On Wed, Sep 16, 2009 at 10:50 AM, Re BR [email protected] wrote:
Sorry that should be
There are two problems here. First, the a[i…-1] concept needs the array
from a.split(‘.’) to work and therefore you need to store it. Also, the
map
doesn’t give you the indexes which is what the i is here. The a[i…-1]
works
by creating an array of each element till the end so you want the index
which is not what the .map in this case will give you.
I’m not sure there is a one liner method going down this road. Maybe the
regex options would work that way but not the split and rejoin concept
(at
least I couldn’t get it there).
John
Hi,
Am Donnerstag, 17. Sep 2009, 01:01:09 +0900 schrieb Re BR:
I have a string “apple-orange-grape”. I need to create a array that will
look like
[“apple-orange-grape”,“orange-grape”,“grape”]
a = []
“apple-orange-grape”.scan /\A|-/ do a.push $’ end
a
Bertram
Hi –
On Thu, 17 Sep 2009, John W Higgins wrote:
regex options would work that way but not the split and rejoin concept (at
least I couldn’t get it there).
Well, hardly worth the trouble, but:
str.split(’-’).inject([]){|acc,s|acc<<[acc[-1],s].compact.join(’-’)}.reverse
David
Re BR: I sent you an e-mail to your hotmail.com mailbox - could you
reply? Might be in your spam folder.
Re BR wrote:
[email protected] Norman wrote:
Re BR,
I noticed in your original statement of the problem you said the input
string was “apple-orange-grape” – so why are you splitting on ‘.’ in
your sample solution?That would be appropriate for, oh, I don’t know, parts of a domain name,
but not for “apple-orange-grape”Re BR wrote:
Hi John,
I pasted the wrong line I meant to say
a.split(‘-’).map{ |i| a[i…-1].join(‘-’) }
At 2009-09-16 02:30PM, “David A. Black” wrote:
str.split(’-’).inject([]){|acc,s|acc<<[acc[-1],s].compact.join(’-’)}.reverse
=> ["apple-orange-banana", "apple-orange", "apple"]
Not quite:
str.split('-').reverse.inject([]){|acc,s|acc<<[s,acc[-1]].compact.join('-')}.reverse
=> ["apple-orange-banana", "orange-banana", "banana"]
At 2009-09-16 02:03PM, “Re BR” wrote:
Re BR wrote:
John W Higgins wrote:
x = “apple-orange-grape”
tmp = x.split(’-’)
(0…tmp.length-1).map{ |i| tmp[i…-1].join(’-’) }trying to make this as a one liner
[…]
a.split(’-’).map{ |i| a[i…-1].join(’-’) }
Ugly, but:
a = "apple-orange-banana"
(tmp = a.split('-')).each_index.collect {|i| tmp[i..-1].join('-')}
trying to make this as a one liner
a.split(’.’).map{ |i| a[i…-1].join(’.’) }
but I am missing something
p (1…str.split(/-/).length).map{|f| str.split(/-/,f)[-1]}
Harry
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.
Sponsor our Newsletter | Privacy Policy | Terms of Service | Remote Ruby Jobs