Reputation: 563
I'm reading a file (input.txt) and trying to split the contents with '*' as the delimiter and below is the code I came up with but it assigns all the values to the first list i.e. uno instead of splitting equally. Where I'm doing it wrong. Many thanks for any help.
One more scenario - how to split the contents equally even though we have no idea how many lines are separated with * .In other words, here 3 rows are separated with the delimiter of multiple *s. What if in case of 1000 + rows and 1000 + delimiter of multiple *s.
input.txt
Record: C:/my_files/00_Roll_Tom-values.txt
#RakeBoss-Random as on 12/19/2016
[groups]
met = chk\rel_io_chk, chk\dev_op_io, chk\yuijn7, chk\rft65y
div = chk\ret5cf, chk\plo09i, chk\czo987, chk\lopikm, chk\qzt0kc
rakeonly = chk\r7ynsd, chk\bhtw5h, chk\ko9ibv, chk\plot9q
*******************************************************************************************
Record: C:/my_files/Docker_red-values.txt
#RakeBoss-Check it io.
[groups]
met = chk\rel_io_chk, chk\dev_op_io, chk\yuijn7
div = chk\gzlqvg, chk\k7ygyp, chk\lzg0rp, chk\oli6iv
rakeonly = chk\qzliu0, chk\pl6w6t, chk\bzp0l, chk\dzfbhp
*******************************************************************************************
Record: C:/my_files/456_Milo_123-values.txt
#RakeBoss-Jan 21st Prod
[groups]
met = chk\rel_io_chk, chk\dev_op_io
div = chk\ret5cf, chk\plo09i, chk\cz0o9t, chk\lopikm
rakeonly = chk\ztylcd, chk\hrft9l, chk\zzkkmi
*******************************************************************************************
Code,
file = File.open("C:/rake_check/input.txt", "r")
file.each_line.map do |line|
next if line.chomp! =~ /^$|#/
uno, deus, id = line.split(/\*+/) # also tried line.split("*")
puts second
end
Upvotes: 0
Views: 222
Reputation: 198556
myarray = File.read('input.txt').split(/^\*{91}\n/)
(or, for example, /^\*{5,}\n/
"split on a line of five or more asterisks" if you don't have exactly 91 each time.)
/\*+/
means "one or more asterisks", which has several dangers: you might get an asterisk somewhere inside the record and split on it, and you're not slurping up the newline so each record except the first will start off with one.
Upvotes: 1