Array to string conversion

Hi, I have 2 arrays(which is part of the hash):

ex = {}
ex[0] = [“xls”, “ini”, “20080326”]
ex[1] = [“gif”, “xls”, “rb”]

All i managed to convert is(using .to_s):
str[0] = “xlsini20080326”
str[1] = “gifxlsrb”

but i want to convert each array into a string, something like
str[0] = “xls,ini,20080326”
str[1] = “gif,xls,rb”

where they are seperated by a comma… Any clean way of doin this?

fr [mailto:[email protected]]

All i managed to convert is(using .to_s):

str[0] = “xlsini20080326”

str[1] = “gifxlsrb”

but i want to convert each array into a string, something like

str[0] = “xls,ini,20080326”

str[1] = “gif,xls,rb”

[email protected]:~$ qri array.join
------------------------------------------------------------- Array#join
array.join(sep=$,) -> str

 Returns a string created by converting each element of the array
 to a string, separated by sep.

    [ "a", "b", "c" ].join        #=> "abc"
    [ "a", "b", "c" ].join("-")   #=> "a-b-c"

[email protected]:~$ irb
irb(main):003:0> ex[0] = [“xls”, “ini”, “20080326”]
=> [“xls”, “ini”, “20080326”]
irb(main):005:0> str[0]=ex[0].join(",")
=> “xls,ini,20080326”

kind regards -botp

On Friday 13 June 2008, Clement Ow wrote:

but i want to convert each array into a string, something like
str[0] = “xls,ini,20080326”
str[1] = “gif,xls,rb”

where they are seperated by a comma… Any clean way of doin this?

ex[0].join ‘,’

Stefano

Try this:
irb(main):004:0> ex
=> {0=>[“xls”, “ini”, “20080326”], 1=>[“gif”, “xls”, “rb”]}
irb(main):005:0> ex = Hash[*ex.collect{|a,b| [a,b.join(",")]}.flatten]
=> {0=>“xls,ini,20080326”, 1=>“gif,xls,rb”}

Sandro

On Fri, Jun 13, 2008 at 5:53 AM, Stefano C.
[email protected]

Sandro P. wrote:

Try this:
irb(main):004:0> ex
=> {0=>[“xls”, “ini”, “20080326”], 1=>[“gif”, “xls”, “rb”]}
irb(main):005:0> ex = Hash[*ex.collect{|a,b| [a,b.join(",")]}.flatten]
=> {0=>“xls,ini,20080326”, 1=>“gif,xls,rb”}

Sandro

On Fri, Jun 13, 2008 at 5:53 AM, Stefano C.
[email protected]

It works! Thanks guys for the input! :wink:

From: Sandro P. [mailto:[email protected]]

irb(main):004:0> ex

=> {0=>[“xls”, “ini”, “20080326”], 1=>[“gif”, “xls”, “rb”]}

irb(main):005:0> ex = Hash[*ex.collect{|a,b| [a,b.join(",")]}.flatten]

=> {0=>“xls,ini,20080326”, 1=>“gif,xls,rb”}

:wink:

irb(main):018:0> ex.inject({}){|h,(v,k)| h[v]=k.join(",");h}
=> {0=>“xls,ini,20080326”, 1=>“gif,xls,rb”}

kind regards -botp

I’m not exactly sure what’s up with your hash wrapper. I think it
would be easier as an array wrapper, because indexes would be implicit
to the object, but hopefully this will do the trick; it might be the
easiest way:

ex.map {|i,v| v * “,” }
=> [“xls,ini,20080326”, “gif,xls,rb”]

From: [email protected] [mailto:[email protected]]

ex.map {|i,v| v * “,” }

i always forget that “*” op on arrays
thanks for the reminder :slight_smile:

kind regards -botp

Peña, Botp wrote:

From: [email protected] [mailto:[email protected]]

ex.map {|i,v| v * “,” }

i always forget that “*” op on arrays
thanks for the reminder :slight_smile:

kind regards -botp

Thank you guys, for your kind inputs! =) it really helped me alot!

However, I now encounter another problem…
Assuming my hash:
ex = {}
ex[0] = [".xls", “.ini”, “20080326”]
ex[1] = [“RVG”, “.xls”, “.rb”]

I need to extract the values in each array with the extentions(values
with periods) into one string and the dir names(values without the
periods) into another string. I’ve been tryin to figure out but to no
avail, is there a clean way to do that?

Peña, Botp wrote:

From: [email protected]

However, I now encounter another problem…

Assuming my hash:

ex = {}

ex[0] = [".xls", “.ini”, “20080326”]

ex[1] = [“RVG”, “.xls”, “.rb”]

I need to extract the values in each array with the extentions(values

with periods) into one string and the dir names(values without the

periods) into another string. I’ve been tryin to figure out but to no

avail, is there a clean way to do that?

Hi Clement, pardon me because i cannot picture the problem.
can you give an example?

Hi botp,

Assuming I have my hash:

ex = {}
ex[0] = [".xls", “.ini”, “20080326”]
ex[1] = [“RVG”, “.xls”, “.rb”]

The result i would want:
str1 = [".xls", “.ini”, “.xls”, “.rb”]
str2 = [“20080326”, “RVG”]

Is there any way i can do this?
Thanks.

Regards,
Clement

On Monday 16 June 2008, Clement Ow wrote:

str2 = [“20080326”, “RVG”]

Is there any way i can do this?
Thanks.

Regards,
Clement

Try looking at Enumerable#partition. A possible way to do this is:

res = ex.values.flatten.partition{|v| v[0…0] == ‘.’}
p res
=> [[".xls", “.ini”, “.xls”, “.rb”], [“20080326”, “RVG”]]

Stefano

From: [email protected]

However, I now encounter another problem…

Assuming my hash:

ex = {}

ex[0] = [".xls", “.ini”, “20080326”]

ex[1] = [“RVG”, “.xls”, “.rb”]

I need to extract the values in each array with the extentions(values

with periods) into one string and the dir names(values without the

periods) into another string. I’ve been tryin to figure out but to no

avail, is there a clean way to do that?

Hi Clement, pardon me because i cannot picture the problem.
can you give an example?

kind regards -botp

From: Clement Ow [mailto:[email protected]]

ex = {}

ex[0] = [".xls", “.ini”, “20080326”]

ex[1] = [“RVG”, “.xls”, “.rb”]

The result i would want:

str1 = [".xls", “.ini”, “.xls”, “.rb”]

str2 = [“20080326”, “RVG”]

Hi Clement, there are many ways.
ff is just one way,

ex = {}
#=> {}
ex[0] = [".xls", “.ini”, “20080326”]
#=> [".xls", “.ini”, “20080326”]
ex[1] = [“RVG”, “.xls”, “.rb”]
#=> [“RVG”, “.xls”, “.rb”]

ex.values
#=> [[".xls", “.ini”, “20080326”], [“RVG”, “.xls”, “.rb”]]

so,

str1,str2=ex.values.flatten.partition{|x| x=~/./}
#=> [[".xls", “.ini”, “.xls”, “.rb”], [“20080326”, “RVG”]]

str1
#=> [".xls", “.ini”, “.xls”, “.rb”]
str2
#=> [“20080326”, “RVG”]

kind regards -botp

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs