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:removed_email_address@domain.invalid]

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”

botp@botp-desktop:~$ 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"

botp@botp-desktop:~$ 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.
removed_email_address@domain.invalid

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.
removed_email_address@domain.invalid

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

From: Sandro P. [mailto:removed_email_address@domain.invalid]

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: removed_email_address@domain.invalid [mailto:removed_email_address@domain.invalid]

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: removed_email_address@domain.invalid [mailto:removed_email_address@domain.invalid]

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: removed_email_address@domain.invalid

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: removed_email_address@domain.invalid

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:removed_email_address@domain.invalid]

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

Many people do not have info on topic like spousal maintenance, i liked it. Thanks for sharing. If you are looking for the top Freelance web developer, look no further as I am providing the best Freelance web designing services ever in town! Here you will get all the top responsive websites with easy navigation accompanied with the latest trends and designs, the top Web developer and Web Designer is known for its exceptional techniques worldwide. As a Web Developer in NYC I will promote your company and its product and services like no other!