From: William James Date: 2005-08-17T06:51:14+09:00 Subject: Re: Probably missing something simple here ... help {wimper} Wilson Bilkovich wrote: > On 8/16/05, B. Angell wrote: > > Am trying to read from a file and write to a number of files based on > > the first 4 letters of the data line. Therefore, I want to be able to > > write the line: A0037775830|lkajsdlkfjsaljf;lsakjfdsa;jf to file A003 > > *and* append all of the A003 lines as well. Here is the hack I have > > below, however, produces errors and I know I am missing something > > simple/easy ..... as follows: > > > > #!/usr/bin/ruby -w > > File.open("meshdata.txt") do |file| > > while line = file.gets > > a = line > > b = a[0,4] > > File.open(b,"w") do |afile| > > puts a.afile > > > > end > > end > > end > > I'm at the end of a long day here, and I haven't bothered to try to > understand your goals/requirements/etc, so forgive me if this is just > a senseless babble. However, here's what comes to mind: > > #!/usr/bin/ruby -w > File.open("meshdata.txt").each_line do |line| > thingy = line[0,4] > File.open(thingy,"a+") do |new_file| > new_file.puts line > end > end > > If your input file is hooooj, you would probably want to sort it > first, to minimize the number of times you open and close files. Doesn't this open and close a file every time a line is read from meshdata.txt, even if that file is sorted?