新行列出到数组 [英] new lines list into array

查看:80
本文介绍了新行列出到数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有新线路的列表,我喜欢它转换成一个阵列,例如

  JANFEBMARAPR可能

[JAN,FEB,MAR,年利率,可]

任何帮助将AP preciated。
谢谢

编辑:

这样的犯规事情似乎工作(text_file.txt包含如上个月的列表)

 文件= File.new(text_file.txt,R)
而(行= file.gets)
    line.chomp
    清单= line.split(/ \\ n /)
    把#{list}里
结束


解决方案

这适用于1.9 ..不知道,如果是空的?可在1.8,虽然

 %(
JANFEBMARAPR可能
。).split(/ \\ n /)拒绝(安培;:空)?

I have a list with new lines and I like to convert it into an array, e.g.

JAN 

FEB 

MAR

APR

MAY

into ["JAN", "FEB", "MAR", "APR", "MAY]

Any help will be appreciated. Thanks

EDIT:

Something like this doesnt seem to work (text_file.txt contains a list of months as above)

file = File.new("text_file.txt", "r")
while (line = file.gets)
    line.chomp 
    list = line.split(/\n/)
    puts "#{list}"
end

解决方案

This works on 1.9.. not sure if empty? is available in 1.8 though

%(
JAN 

FEB 

MAR

APR

MAY
).split(/\n/).reject(&:empty?)

这篇关于新行列出到数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆