On Thu, 13 Jul 2006 dblack / wobblini.net wrote:

> OK, but how do you know whether or not this is valid?
>
>  3:ab23:cat

it is valid.


harp:~ > cat a.rb
require 'strscan'

class List < ::Array
   def initialize s
     parse s
   end
   class ParseError < ::StandardError; end
   def parse s
     clear
     ss = StringScanner.new s.to_s
     until ss.eos?
       n = ss.scan %r/^\d+:/
       raise ParseError, "syntax error beginning @ pos <#{ ss.pos }>" unless n
       n = n.to_i
       raise RangeError, "bad length <#{ n }>" if n < 0
       word = ss.scan(%r/.{0,#{ n }}/).to_s
       raise ParseError, "syntax error beginning @ pos <#{ ss.pos }>" unless
         word.size == n
       self << word
     end
   end
end

list = List.new "3:ab23:cat5:sheep"
p list

list = List.new "3:ab23:cat"
p list

harp:~ > ruby a.rb
["ab2", "cat", "sheep"]
["ab2", "cat"]


regards.

-a
-- 
suffering increases your inner strength.  also, the wishing for suffering
makes the suffering disappear.
- h.h. the 14th dali lama