开发者

Splitting in ruby from file

While reading from a file I need to be able to chunk it into smaller pieces.

Basically the file looks like this:

6057493131
    Test 1
    Test 2
    Test 3
6057493132
    Test 1
    Test 2
    Test 3
605749开发者_Python百科3133
    Test 1
    Test 2
    Test 3
6057493134
    Test 1
    Test 2
    Test 3

I need it to split everytime a new numberseries is beginning.

I've use this code:

f = File.open("test.txt")  
     fread = f.read  
     chunk = fread.split(/^[0-9]/)  
puts chunk[0...3]

It works but frustrating enough the first number is missing. ex. "6057493132" becomes "057493132" How do I get all the numbers, or any better idea on howto do it?


To just get the numbers use scan instead of split.

chunk = fread.scan(/^[0-9]+/) 

To also get the text in between you can use split with a lookahead:

chunk = fread.split(/\n(?=[0-9])/)


You could split the string with a zero-width positive look-ahead assertion and then just #to_a on each element. This will give you a "two dimensional" array with each row being one group.

require 'pp'
pp(IO.read('split.txt').split(/(?=^\d)/).map(&:to_a))

[["6057493131\n", "    Test 1\n", "    Test 2\n", "    Test 3\n"],
 ["6057493132\n", "    Test 1\n", "    Test 2\n", "    Test 3\n"],
 ["6057493133\n", "    Test 1\n", "    Test 2\n", "    Test 3\n"],
 ["6057493134\n", "    Test 1\n", "    Test 2\n", "    Test 3\n"]]


If there are varying amounts of tests, go with DigitalRoss. If there are always three of them, have a look at this:

ar = DATA.map{|line|line.strip}   # puts all in an array, getting rid of the whitespace
ar.each_slice(4){ |number, *tests| puts "Run #{number} has #{tests.inspect}" }

__END__
6057493131
    Test 1
    Test 2
    Test 3
6057493132
    Test 1
    Test 2
    Test 3
6057493133
    Test 1
    Test 2
    Test 3
6057493134
    Test 1
    Test 2
    Test 3

The stuff after __END__ is treated like a file named DATA. Output:

Run 6057493131 has ["Test 1", "Test 2", "Test 3"]
Run 6057493132 has ["Test 1", "Test 2", "Test 3"]
Run 6057493133 has ["Test 1", "Test 2", "Test 3"]
Run 6057493134 has ["Test 1", "Test 2", "Test 3"]
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜