From: Panu Kinnari Date: 2008-10-04T14:34:22+09:00 Subject: Re: Parsing non-delimited text file i3w wrote: > It is really easy to re-factor from array based to file based. > Change the first three lines of your code to this... Yes, thats what I did. Finished next iteration of my code yesterday. Now it basically does everything I need and writes new lines to text file. What I'd want to do next is convert year-week-weekday combo to proper timestamp, but haven't looked into it yet. Anyway, comments are again appreciated. # Write orderline to file def writefile(file, *linedata) linedata.each do |line| file << line.join(",") + "\n" end end # Read file line-by-line and extract relevant information def readfile(file, outputfile) out = File.new(outputfile, "w+") info = [] wline = ['year', 'week', 'day', 'customerid', 'customer', 'subcustid', 'subcust', 'prodid', 'prod', 'qty'] IO.foreach(file){|line| if line =~ /Customer/ wline[3] = line.split(":")[1].scan(/\d+/) wline[4] = line.split(":")[1].scan(/[a-zA-Z]+/).join(" ") elsif line =~ /Sub-cust/ wline[5] = line.split(":")[1].scan(/\d+/) wline[6] = line.split(":")[1].scan(/[a-zA-Z]+/).join(" ") elsif line =~ /Year/ wline[0] = line.scan(/\d+/)[0] elsif line =~ /Week/ wline[1] = line.scan(/\d+/)[0] elsif line.strip! =~ /\A\d/ wline[7] = line.scan(/\A\d+/) temp = line.scan(/[A-Za-z]+/) wline[2] = temp.pop #later used for delivery day wline[8] = temp.join(" ") wline[9] = line.scan(/\d+/)[line.scan(/\d+/).length-13] writefile(out, wline) elsif line =~ /MON/ || line =~ /TUE/ || line =~ /WED/ || line =~ /THU/ || line =~ /FRI/ || line =~ /SAT/ || line =~ /SUN/ wline[2] = line.scan(/[A-Za-z]+/) wline[9] = line.scan(/\d+/)[line.scan(/\d+/).length-13] writefile(out, wline) end } out.close end readfile('source_example', 'output.txt') -- Posted via http://www.ruby-forum.com/.