I made a small mistake

change
stack[-1] += count.to_s
to
stack[-1] += count.to_s if stack[-1]

On Dec 14, 2007 7:44 PM, Joe <qbproger / gmail.com> wrote:
>
> hey,
>
> I didn't test this too well, but it works for your string.  I doubt
> it's the most clever way to do it, but it should get the job done.
>
> stack = Array.new
> input = "z.methodx(z.methody(),z.methodz(z.methodx))"
>
> last = 0
> count = 0
>
> while last < input.length
>         cur = input.index(/[\)\(,]/, last)
>         if cur then
>                 b = input[cur,1]
>                 if (/[\(,]/.match(input[last-1,1]) &&
> /[\),]/.match(b)) && cur-last > 1 then
>                         count += 1
>                         puts count.to_s + " " + input[last, cur-last]
>                         stack[-1] += count.to_s
>                 end
>
>                 case b
>                         when '('
>                                 stack.push(input[last, cur-last] + "(")
>                         when ')'
>                                 count += 1
>                                 puts count.to_s + " " + stack.pop + ")"
>                                 stack[-1] += count.to_s
>                         when ','
>                                 stack[-1] += ','
>                 end
>
>                 last = cur+1
>         end
> end
>
> Joe
>
> On Dec 14, 2007 9:03 AM, Saladin Mundi <saladin.mundi / gmx.de> wrote:
>
> > hey guys
> >
> > I'm trying to split an string (which contains various method calls).
> > The result of the splitting of the string should be an array (or
> > something like that) which contains a sequence which an interpreter
> > would have called the methods.
> > Example
> >
> > class X
> >    def test(input)
> >      ...
> >    end
> > end
> >
> > x.new.test("z.methodx(z.methody(),z.methodz(z.methodx))")
> >
> >
> > If I pass "z.methodx(z.methody(),z.methodz(z.methodx))" there should be
> > afterwards an arry like this:
> >
> >  [0] -> var1 = z.methody()
> >  [1] -> var2 = z.methodx
> >  [2] -> var3 = z.methodz(var2)
> >  [3] -> z.methodx(var1,var3)
> >
> > so the call(s on different methods) given by an input string shall be
> > extracted into the real execution sequence like an interpreter would
> > excecute this string it.
> >
> > I hope you can help me with my problem.
> > --
> > Posted via http://www.ruby-forum.com/.
> >
> >
>
>