Hi --

On Sat, 7 Feb 2009, Tom Cloyd wrote:

> Tom Cloyd wrote:
>> I'm baffled by this strange outcome - I cannot reduce multiple spaces from 
>> a text file. This isn't just a regex problem, somehow. I'm failing to grasp 
>> something essential, but don't know what it is. All help appreciated, as 
>> usual!
>> 
>> Here is a demo of my problem, in which I try two different ways, and both 
>> fail:
>> 
>> === code ===
>> # h2t.rb
>> 
>> def main
>>  # conversion table spec
>>  conv = [
>>  [ '<h1>', 'h1. ' ], [ '<h2>', 'h2. ' ], [ '<h3>', 'h3. ' ],
>>  [ '<h4>', 'h4. ' ], [ '<h5>', 'h5. ' ], [ '<h6>', 'h6. ' ],  [ /<\/h\d>/, 
>> '' ],
>>  [ " +", ' ' ]]  # <= this last array element should do the trick, but 
>> doesn't
> Ouch. THIS - [ / +/, ' ' ], substituted for [ " +", ' ' ] above fixes it. I'm 
> going blind, obviously.

Just for fun, here's another way to write the method:

def main
  data = File.read("tom.txt")
  data.gsub!(/<(h[1-6])>/, "\\1. ")
  data.gsub!(/<\/h\d>/, "")
  data.squeeze!(' ')

  open("tom.out", "w") {|f| f.write(data) }

end

I think that does the same thing. Tweak to taste :-)


David

-- 
David A. Black / Ruby Power and Light, LLC
Ruby/Rails consulting & training: http://www.rubypal.com
Coming in 2009: The Well-Grounded Rubyist (http://manning.com/black2)

http://www.wishsight.com => Independent, social wishlist management!