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?

···

--
Posted via http://www.ruby-forum.com/.

fr [mailto:clement.ow@asia.bnpparibas.com]
# 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

ex[0].join ','

Stefano

···

On Friday 13 June 2008, Clement Ow wrote:

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?

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

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 Crocco <stefano.crocco@alice.it> wrote:

On Friday 13 June 2008, Clement Ow wrote:
> 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?

ex[0].join ','

Stefano

--
Go outside! The graphics are amazing!

# ex.map {|i,v| v * "," }

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

kind regards -botp

···

From: alandacosta@gmail.com [mailto:alandacosta@gmail.com]

Sandro Paganotti 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 Crocco
<stefano.crocco@alice.it>

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

···

--
Posted via http://www.ruby-forum.com/\.

# 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

···

From: Sandro Paganotti [mailto:sandro.paganotti@gmail.com]

Peña, Botp wrote:

From: alandacosta@gmail.com [mailto:alandacosta@gmail.com]
# 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?

···

--
Posted via http://www.ruby-forum.com/\.

# 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@asia.bnpparibas.com

Peña, Botp wrote:

From: clement.ow@asia.bnpparibas.com
# 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

···

--
Posted via http://www.ruby-forum.com/\.

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

···

On Monday 16 June 2008, Clement Ow wrote:

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

# 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

···

From: Clement Ow [mailto:clement.ow@asia.bnpparibas.com]