array to string conversion

C

Clement Ow

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?
 
S

Stefano Crocco

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
 
S

Sandro Paganotti

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

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

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
 
C

Clement Ow

Sandro said:
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
<[email protected]>

It works! Thanks guys for the input! ;)
 
P

Peña, Botp

From: Sandro Paganotti [mailto:[email protected]]=20
# irb(main):004:0> ex
# =3D> {0=3D>["xls", "ini", "20080326"], 1=3D>["gif", "xls", "rb"]}
# irb(main):005:0> ex =3D Hash[*ex.collect{|a,b| =
[a,b.join(",")]}.flatten]
# =3D> {0=3D>"xls,ini,20080326", 1=3D>"gif,xls,rb"}

;)

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


kind regards -botp
 
A

alandacosta

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

Peña, Botp

From: (e-mail address removed) [mailto:[email protected]]=20
# ex.map {|i,v| v * "," }

i always forget that "*" op on arrays=20
thanks for the reminder :)

kind regards -botp
 
C

Clement Ow

From: (e-mail address removed) [mailto:[email protected]]
# ex.map {|i,v| v * "," }

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

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?
 
P

Peña, Botp

RnJvbTogY2xlbWVudC5vd0Bhc2lhLmJucHBhcmliYXMuY29tIA0KIyBIb3dldmVyLCBJIG5vdyBl
bmNvdW50ZXIgYW5vdGhlciBwcm9ibGVtLi4NCiMgQXNzdW1pbmcgbXkgaGFzaDoNCiMgZXggPSB7
fQ0KIyBleFswXSA9IFsiLnhscyIsICIuaW5pIiwgIjIwMDgwMzI2Il0NCiMgZXhbMV0gPSBbIlJW
RyIsICIueGxzIiwgIi5yYiJdDQojIEkgbmVlZCB0byBleHRyYWN0IHRoZSB2YWx1ZXMgaW4gZWFj
aCBhcnJheSB3aXRoIHRoZSBleHRlbnRpb25zKHZhbHVlcyANCiMgd2l0aCBwZXJpb2RzKSBpbnRv
IG9uZSBzdHJpbmcgYW5kIHRoZSBkaXIgbmFtZXModmFsdWVzIHdpdGhvdXQgdGhlIA0KIyBwZXJp
b2RzKSBpbnRvIGFub3RoZXIgc3RyaW5nLiBJJ3ZlIGJlZW4gdHJ5aW4gdG8gZmlndXJlIG91dCBi
dXQgdG8gbm8gDQojIGF2YWlsLCBpcyB0aGVyZSBhIGNsZWFuIHdheSB0byBkbyB0aGF0Pw0KDQpI
aSBDbGVtZW50LCBwYXJkb24gbWUgYmVjYXVzZSBpIGNhbm5vdCBwaWN0dXJlIHRoZSBwcm9ibGVt
Lg0KY2FuIHlvdSBnaXZlIGFuIGV4YW1wbGU/DQoNCmtpbmQgcmVnYXJkcyAtYm90cA0K
 
C

Clement Ow

From: (e-mail address removed)
# 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
 
S

Stefano Crocco

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

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

res = ex.values.flatten.partition{|v| v[0..0] == '.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

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
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top