addis_a
1
Hi!
I want to create something that, in my mind, is array of vertical
arrays. Something like this:
Code Name Gender
1 Anne F
2 John M
3 Jack M
How can i manipulate data inside a multidimensional array so that its
subarrays are printed like this?
I have very little knowledge about Ruby. I’ve tried hard to find an
answer for this on the net, but couldn’t find it.
Thanks in advance for any feedback!
cgraco
2
I hope this is usefull for @Caio Graco
2.0.0-p481 :041 > test = Array.new(3,Array.new(1))
=> [[nil], [nil], [nil]]
2.0.0-p481 :042 > test = %w[[43][sai][M]]
=> ["[43][sai][M]"]
2.0.0-p481 :043 > test = %w[[41][prasanna][F]]
=> ["[41][prasanna][F]"]
cgraco
3
Caio Graco wrote in post #1161260:
Hi!
I want to create something that, in my mind, is array of vertical
arrays. Something like this:
Code Name Gender
1 Anne F
2 John M
3 Jack M
How can i manipulate data inside a multidimensional array so that its
subarrays are printed like this?
I have very little knowledge about Ruby. I’ve tried hard to find an
answer for this on the net, but couldn’t find it.
Thanks in advance for any feedback!
a = %w[code,name,gender]
b = %w[1,sai,f]
=> [“1,sai,f”]
cgraco
4
Caio Graco wrote in post #1161260:
Hi!
I want to create something that, in my mind, is array of vertical
arrays. Something like this:
Code Name Gender
1 Anne F
2 John M
3 Jack M
========================================
data=[
[1,2,3],
%w{Anne Jonh Jack},
%w{F M M}
]
p data
puts “”
(0…data.first.size).each do |i|
puts (0…data.size).map { |col| data[col][i] }.join("\t")
end
ruby e.rb
[[1, 2, 3], [“Anne”, “Jonh”, “Jack”], [“F”, “M”, “M”]]
1 Anne F
2 Jonh M
3 Jack M
cgraco
5
Write this to a file & run it. The output is what you have asked for:
data=[]
new_data=[1,“Anne”,“F”]
data.push(new_data)
new_data=[2,“John”,“M”]
data.push(new_data)
new_data=[3,“Jack”,“M”]
data.push(new_data)
puts “Code\tName\tGender”
data.each do |entry|
entry.each do |value|
print value
print “\t”
end
puts
end
cgraco
6
Regis d’Aubarede wrote in post #1161303:
Caio Graco wrote in post #1161260:
Hi!
I want to create something that, in my mind, is array of vertical
Oups!
array.transpose do the hard work.
So this do the job:
####################################################
data=[
[1,2,3],
%w{Anne Jonh Jack},
%w{F M M}
]
puts data.transpose.map {|line| line.join("\t")}.join("\n")
####################################################