owc
June 13, 2008, 7:47am
1
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?
owc
June 13, 2008, 7:54am
2
fr [mailto:[email protected] ]
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
owc
June 13, 2008, 7:55am
3
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
owc
June 13, 2008, 10:08am
4
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.
[email protected]
owc
June 13, 2008, 10:35am
5
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.
[email protected]
It works! Thanks guys for the input!
owc
June 13, 2008, 10:37am
6
From: Sandro P. [mailto:[email protected] ]
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”}
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
owc
June 13, 2008, 11:59am
7
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”]
owc
June 13, 2008, 12:10pm
8
From: [email protected] [mailto:[email protected] ]
ex.map {|i,v| v * “,” }
i always forget that “*” op on arrays
thanks for the reminder
kind regards -botp
owc
June 16, 2008, 9:46am
9
Peña, Botp wrote:
From: [email protected] [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?
owc
June 16, 2008, 10:38am
10
Peña, Botp wrote:
From: [email protected]
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
owc
June 16, 2008, 10:46am
11
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
owc
June 16, 2008, 9:52am
12
From: [email protected]
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
owc
June 16, 2008, 10:49am
13
From: Clement Ow [mailto:[email protected] ]
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