array to string conversion

Discussion in 'Ruby' started by Clement Ow, Jun 13, 2008.

  1. Clement Ow

    Clement Ow Guest

    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/.
     
    Clement Ow, Jun 13, 2008
    #1
    1. Advertising

  2. 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
     
    Stefano Crocco, Jun 13, 2008
    #2
    1. Advertising

  3. [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 <>
    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!
     
    Sandro Paganotti, Jun 13, 2008
    #3
  4. Clement Ow

    Clement Ow Guest

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


    It works! Thanks guys for the input! ;)
    --
    Posted via http://www.ruby-forum.com/.
     
    Clement Ow, Jun 13, 2008
    #4
  5. Clement Ow

    Peña, Botp Guest

    From: Sandro Paganotti [mailto:]=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
     
    Peña, Botp, Jun 13, 2008
    #5
  6. Clement Ow

    Guest

    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"]
     
    , Jun 13, 2008
    #6
  7. Clement Ow

    Peña, Botp Guest

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

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

    kind regards -botp
     
    Peña, Botp, Jun 13, 2008
    #7
  8. Clement Ow

    Clement Ow Guest

    Peña, Botp wrote:
    > From: [mailto:]
    > # 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-forum.com/.
     
    Clement Ow, Jun 16, 2008
    #8
  9. RnJvbTogY2xlbWVudC5vd0Bhc2lhLmJucHBhcmliYXMuY29tIA0KIyBIb3dldmVyLCBJIG5vdyBl
    bmNvdW50ZXIgYW5vdGhlciBwcm9ibGVtLi4NCiMgQXNzdW1pbmcgbXkgaGFzaDoNCiMgZXggPSB7
    fQ0KIyBleFswXSA9IFsiLnhscyIsICIuaW5pIiwgIjIwMDgwMzI2Il0NCiMgZXhbMV0gPSBbIlJW
    RyIsICIueGxzIiwgIi5yYiJdDQojIEkgbmVlZCB0byBleHRyYWN0IHRoZSB2YWx1ZXMgaW4gZWFj
    aCBhcnJheSB3aXRoIHRoZSBleHRlbnRpb25zKHZhbHVlcyANCiMgd2l0aCBwZXJpb2RzKSBpbnRv
    IG9uZSBzdHJpbmcgYW5kIHRoZSBkaXIgbmFtZXModmFsdWVzIHdpdGhvdXQgdGhlIA0KIyBwZXJp
    b2RzKSBpbnRvIGFub3RoZXIgc3RyaW5nLiBJJ3ZlIGJlZW4gdHJ5aW4gdG8gZmlndXJlIG91dCBi
    dXQgdG8gbm8gDQojIGF2YWlsLCBpcyB0aGVyZSBhIGNsZWFuIHdheSB0byBkbyB0aGF0Pw0KDQpI
    aSBDbGVtZW50LCBwYXJkb24gbWUgYmVjYXVzZSBpIGNhbm5vdCBwaWN0dXJlIHRoZSBwcm9ibGVt
    Lg0KY2FuIHlvdSBnaXZlIGFuIGV4YW1wbGU/DQoNCmtpbmQgcmVnYXJkcyAtYm90cA0K
     
    Peña, Botp, Jun 16, 2008
    #9
  10. Clement Ow

    Clement Ow Guest

    Peña, Botp wrote:
    > From:
    > # 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/.
     
    Clement Ow, Jun 16, 2008
    #10
  11. 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
     
    Stefano Crocco, Jun 16, 2008
    #11
  12. RnJvbTogQ2xlbWVudCBPdyBbbWFpbHRvOmNsZW1lbnQub3dAYXNpYS5ibnBwYXJpYmFzLmNvbV0g
    DQojIGV4ID0ge30NCiMgZXhbMF0gPSBbIi54bHMiLCAiLmluaSIsICIyMDA4MDMyNiJdDQojIGV4
    WzFdID0gWyJSVkciLCAiLnhscyIsICIucmIiXQ0KIyBUaGUgcmVzdWx0IGkgd291bGQgd2FudDoN
    CiMgc3RyMSA9IFsiLnhscyIsICIuaW5pIiwgIi54bHMiLCAiLnJiIl0NCiMgc3RyMiA9IFsiMjAw
    ODAzMjYiLCAiUlZHIl0NCg0KSGkgQ2xlbWVudCwgdGhlcmUgYXJlIG1hbnkgd2F5cy4NCmZmIGlz
    IGp1c3Qgb25lIHdheSwNCg0KZXggPSB7fQ0KIz0+IHt9DQpleFswXSA9IFsiLnhscyIsICIuaW5p
    IiwgIjIwMDgwMzI2Il0NCiM9PiBbIi54bHMiLCAiLmluaSIsICIyMDA4MDMyNiJdDQpleFsxXSA9
    IFsiUlZHIiwgIi54bHMiLCAiLnJiIl0NCiM9PiBbIlJWRyIsICIueGxzIiwgIi5yYiJdDQoNCmV4
    LnZhbHVlcw0KIz0+IFtbIi54bHMiLCAiLmluaSIsICIyMDA4MDMyNiJdLCBbIlJWRyIsICIueGxz
    IiwgIi5yYiJdXQ0KDQpzbywNCg0Kc3RyMSxzdHIyPWV4LnZhbHVlcy5mbGF0dGVuLnBhcnRpdGlv
    bnt8eHwgeD1+L1wuL30NCiM9PiBbWyIueGxzIiwgIi5pbmkiLCAiLnhscyIsICIucmIiXSwgWyIy
    MDA4MDMyNiIsICJSVkciXV0NCg0Kc3RyMQ0KIz0+IFsiLnhscyIsICIuaW5pIiwgIi54bHMiLCAi
    LnJiIl0NCnN0cjINCiM9PiBbIjIwMDgwMzI2IiwgIlJWRyJdDQoNCmtpbmQgcmVnYXJkcyAtYm90
    cA0K
     
    Peña, Botp, Jun 16, 2008
    #12
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gerald Rosenberg

    Q re conversion of string to int array

    Gerald Rosenberg, Apr 11, 2004, in forum: Java
    Replies:
    3
    Views:
    3,255
    Sudsy
    Apr 11, 2004
  2. =?Utf-8?B?UmFqZXNoIHNvbmk=?=

    'System.String[]' from its string representation 'String[] Array'

    =?Utf-8?B?UmFqZXNoIHNvbmk=?=, May 4, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    1,820
    =?Utf-8?B?UmFqZXNoIHNvbmk=?=
    May 4, 2006
  3. Delali Dzirasa
    Replies:
    4
    Views:
    1,046
    Delali Dzirasa
    Sep 15, 2003
  4. , India
    Replies:
    2
    Views:
    509
    Fraser Ross
    Sep 15, 2009
  5. Nathan O.
    Replies:
    2
    Views:
    294
    Nathan O.
    Mar 7, 2006
Loading...

Share This Page