我正在尝试显示我的打印输出,以便"|"
字符输出一次,以分隔我加入的两个数组。我试图弄清楚为什么它会在每个新行上打印"|"
字符。
葡萄,樱桃,李子,李子,橙,李子,樱桃,樱桃,芒果,梨| 3,0,46,3,43,28,32,34,3,34 | 14,49,48,24,12,0
"|"
"14"
不应该在那里。它应该继续打印数字数组。这是我的代码
words = []
numbers = []
File.open(ARGV[0]).each_line do |line|
mix_content = line.scan(/\w+/)
def numeric?(lookAhead)
lookAhead =~ /[A-Za-z]/
end
mix_content.each do |content|
if numeric?(content) == 0
words.push(content)
else
numbers.push(content)
end
end
result = words.join(',') + '|' + numbers.join(',')
puts result
words.clear
numbers.clear
mix_content.clear
end
12,peach,pear,orange,31,mango,3,33,2,41,orange,28,19,cherry,16,36
grapes,16,orange,plum,peach,16,12,grapes,20,44,banana,44,36,38,grapes,pineapple,plum
grapes,40,peach,7,2,8,35,20,grapes,plum,pineapple,banana,11,grapes,6,6,pear,48,7,27
peach,4,grapes,26,0,3,apricot,5,cherry,peach,apricot,pineapple,peach,28,pear,banana
3,0,46,grapes,cherry,plum,plum,3,43,28,orange,plum,cherry,cherry,32,34,3,34,mango,pear
14,49,48,24,12,0
peach,36,24,melon,37,banana,7,50,24,49,15,10,pear,melon,pineapple,melon,24
40,cherry,peach,pear,10,36,apricot,grapes,orange,grapes,apricot,10,plum,orange,peach,35,24
32,3,2,35,28,36,1,27,28,31
plum,pear,12,pineapple,banana,25,pear,melon,1,melon,banana,34,peach,39,plum,banana
peach,36,grapes,48,16,2,apricot,24,banana,26,50,27,peach,pineapple,peach,18,cherry,26,34,37
15,grapes,mango,41,peach,banana,21,plum,10,apricot,6,5,12,37,pineapple,41,pear
4,15,mango,15,cherry,4,29,peach,orange,34,grapes,12,50,42,plum
30,11,35,44,42
18,melon,40,peach,49,peach,peach,37,35,23,plum,banana,plum,mango,melon,pineapple,orange,27,37
cherry,7,cherry,pear,23,mango,3,pear,8,peach,28,pear,28,31,orange,40,grapes,grapes,42
6,grapes,21,36,apricot,38,peach,49,42,29,2,6,38,mango,42,apricot,46,40,20
4,18,10,33,44
apricot,plum,pear,47,grapes,15,10,15,8,38,mango,banana,pineapple,pear,3
8,11,8,50,mango,24,31,8,plum,plum,49,31,orange,banana,46,33,melon,5,grapes,26
25,29,30,30,grapes,17,0,34,22,35,grapes,35,pear,grapes,21,plum,6,27
2,cherry,mango,16,peach,banana,cherry,46,melon,38,46,pear,peach,50,mango,pineapple
grapes,melon,apricot,banana,melon,grapes,cherry,pineapple,peach
36,23,47,7,10,25,40
31,17,40,29,18,2,50,43
pear,melon,48,31,grapes,apricot,grapes,grapes,melon,27,banana,8,apricot,20,mango,banana,cherry,plum,banana,14
plum,38,peach,27,melon,47,9,pear,melon,melon,29,5,apricot,26,mango,orange,41,14,pineapple
16,apricot,26,7,pineapple,24,grapes,15,pineapple,37,apricot,12,mango,49,21,10,18,apricot,30,pineapple
mango,31,peach,43,plum,apricot,26,melon,plum,9,melon,8,banana,6,pineapple,6,2
melon,37,mango,apricot,plum,18,15,35,22,peach,cherry,plum,17,41,40,peach
28,mango,cherry,banana,melon,44,7,7,35,pineapple,peach,mango,peach,12,45
41,37,38,17,40,40,41,4
plum,banana,orange,orange,13,orange,39,24,mango,cherry,23,8,grapes,22,49,40,22,plum,grapes
grapes,33,pineapple,36,38,melon,47,31,31,35,mango,32,mango,0,41
35,41,peach,32,8,11,16,melon,16,43,50,plum,pineapple,42,12,plum,apricot
pineapple,pear,mango,apricot,plum
plum,mango,0,pineapple,pear,orange,mango,16,mango,40,pear,plum,peach,mango,41,2,24
mango,34,23,34,41,plum,cherry,orange,1,peach,18,apricot,28,49,grapes,pear,16,4,banana
10,11,grapes,melon,11,grapes,plum,grapes,30,plum,banana,33,22,25,21,banana,peach,cherry
melon,32,mango,27,grapes,apricot,47,14,39,apricot,0,21,melon,mango,grapes,plum,48,mango,36
答案 0 :(得分:2)
您正在一次处理一行,而某些行没有单词。像这样的一行:
3,0,46,grapes,cherry,plum,plum,3,43,28,orange,plum,cherry,cherry,32,34,3,34,mango,pear
会给:
grapes,cherry,plum,plum,orange,plum,cherry,cherry,mango,pear|3,0,46,3,43,28,32,34,3,34
但是有一句话:
14,49,48,24,12,0
没有单词,因此当您设置words.join(',')
时,result
为空。
答案 1 :(得分:0)
" |"通过" 14"不应该在那里。它应该继续打印 一系列数字。这是我的代码
而不是:
result = words.join(',') + '|' + numbers.join(',')
请这样做:
result = [words.join(','), numbers.join(',')].select{ |e| not e.empty? }.join('|')
或者
result = [words.join(','), numbers.join(',')].reject(&:empty?).join('|')