From: matt@... (matt neuburg) Date: 2007-09-20T02:50:04+09:00 Subject: Re: errors, trying to edit and write some text files Nathan Thickett-menghini wrote: > HOME: > [18/09/2007] Parcelforce coming after 1pm > [19/09/2007] be a bender > [21/09/2007] Sort important documents etc > > WORK: > [24/09/2007] Learn NATS Stuff > > CAR: > [30/09/2007] nob > > So you see there are lines that begin with letters (these are the > contexts, Home, Work, etc) lines with a small gap followed by events, > and lines that are entirely blank. > > You'll have noticed that part of the script deletes duplicate lines - > so that you don't get an event that is on today's feed repeated on the > weekly feed. This works mostly but it also deletes repeated CONTEXTS, > i.e. the context 'HOME:' will appear on today's feed but not on any > others. I've tried to get round this by totally abandoning all contexts, > i.e. changing the part that parses the lines to NOT INCLUDE lines that > begin with word characters. This works, but it now still includes the > totally empty lines, which puts gaps in my text files. Could you add a In my view the entire original script is misconceived. You can't just look at individual lines. You need to cycle thru the lines in order, gathering info as categorized. Something, perhaps, like this: cats = {} today = `curl -s "#{URL}"?due=0` today.split("\n").inject do |memo, line| line.lstrip! if line =~ /^\w*:/ cats[line] = [] line else cats[memo] |= line.to_a memo end end Now "cats" holds a nice hash where the keys are the categories and each category's value is an array of its events. Do that again for "tomorrow" and the others, using the same "cats"; no duplicate values will be inserted, because of the use of the |= operator. When you're all finished, output "cats" in whatever format you like, such as: cats.each do |cat, events| puts cat events.each do |event| puts " " + event end end If you wanted to get really neat, you could sort stuff before outputting. m. -- matt neuburg, phd = matt@tidbits.com, http://www.tidbits.com/matt/ Tiger - http://www.takecontrolbooks.com/tiger-customizing.html AppleScript - http://www.amazon.com/gp/product/0596102119 Read TidBITS! It's free and smart. http://www.tidbits.com