简体   繁体   English

在 Ruby 的控制台中的一行上打印一组数组(每个主数组对象一行)

[英]Printing an array of arrays on one line in console (one line per master array object) in Ruby

I have an array of arrays that is currently printing each object in the array on its own line.我有一个数组数组,它​​当前在自己的行上打印数组中的每个对象。 The master array holds many different people inside of it.主阵内容纳了许多不同的人。 Each person has 5 different objects stored to them (eg Last Name, First Name, DOB.. etc)每个人都存储了 5 个不同的对象(例如姓氏、名字、出生日期等)

Kournikova
Anna
F
6/3/1975
Red

Hingis
Martina
F
4/2/1979
Green

Seles
Monica
F
12/2/1973
Black

What I'm trying to do is print out each person and their corresponding objects on one line, per person.我想要做的是在一行上打印出每个人及其相应的对象。

Does anyone have a solution for this?有没有人对此有解决方案? Additionally, the output should not contain array brackets ( [] ) or commas.此外,输出不应包含数组括号 ( [] ) 或逗号。 I'm wondering if it will simply need to be a string, or if there is something I am missing.我想知道它是否只需要一个字符串,或者是否有我遗漏的东西。

Some of my code below:我的一些代码如下:

space_array = [split_space[0],split_space[1],split_space[3],new_date,split_space[5]]
master << space_array 
puts master

The ideal output would be something like this:理想的输出是这样的:

Kournikova Anna F 6/3/1975 Red
Hingis Martina F 4/2/1979 Green
Seles Monica F 12/2/1973 Black
your_array.each do |person|
  puts person.join(" ")
end

The method puts will automatically put a new line. puts 方法会自动换行。 Use print instead to print the text out with no new line.使用 print 代替打印文本而不换行。

Or if you want, you can use the join function.或者,如果需要,您可以使用 join 功能。

['a', 'b', 'c'].join(' ') 
=> 'a b c'

You can just iterate over the outer array and join the inner arrays into a string.你可以只迭代外阵列过来, join内部数组到一个字符串。 Since you provide no example data ready for copying and pasting, here's some example code I made up:由于您没有提供可供复制和粘贴的示例数据,以下是我编写的一些示例代码:

outer_array.each { |inner| puts inner.join(' ') }

更简单:

puts your_array.join(" ")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM