[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

array to string conversion

Clement Ow

6/13/2008 5:47:00 AM

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

11 Answers

Stefano Crocco

6/13/2008 5:54:00 AM

0

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


Sandro Paganotti

6/13/2008 8:08:00 AM

0

[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

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!

Clement Ow

6/13/2008 8:35:00 AM

0

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! ;)
--
Posted via http://www.ruby-....

Peña, Botp

6/13/2008 8:36:00 AM

0

From: Sandro Paganotti [mailto:sandro.paganotti@gmail.com]=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

alandacosta@gmail.com

6/13/2008 9:59:00 AM

0

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

Peña, Botp

6/13/2008 10:10:00 AM

0

From: alandacosta@gmail.com [mailto:alandacosta@gmail.com]=20
# ex.map {|i,v| v * "," }

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

kind regards -botp

Clement Ow

6/16/2008 7:46:00 AM

0

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

Peña, Botp

6/16/2008 7:51:00 AM

0

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

Clement Ow

6/16/2008 8:38:00 AM

0

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

Stefano Crocco

6/16/2008 8:45:00 AM

0

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

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