string to array

R

Re BR

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
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Good Morning,

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
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

Good Morning,

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('-') }

Sorry that should be

(0..tmp.length-1).map{ |i| tmp[i..-1].join('-') }

John
 
R

Re BR

John said:
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
 
7

7stud --

Re said:
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

1)
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"]


2)
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.
 
J

John

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 said:
John said:
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
 
R

Re BR

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 said:
John said:
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('-') }
 
B

Bertram Scharpf

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
 
J

John W Higgins

[Note: parts of this message were removed to make it a legal post.]

John said:
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

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
 
J

John

Re BR: I sent you an e-mail to your hotmail.com mailbox - could you
reply? Might be in your spam folder.

Re said:
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('-') }
 
D

David A. Black

Hi --

John said:
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

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).

Well, hardly worth the trouble, but:

str.split('-').inject([]){|acc,s|acc<<[acc[-1],s].compact.join('-')}.reverse

:)


David
 
H

Harry Kakueki

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,169
Messages
2,570,916
Members
47,458
Latest member
Chris#

Latest Threads

Top