Re: ex.exe hangs in a while loop

new topic     » goto parent     » topic index » view thread      » older message » newer message

Subject: Re: ex.exe hangs in a while loop




Oops! Punched a wrong button half way thorugh my post:

My 2.5 interpreter hangs (even the trace() debugger hangs) with this code:

include get.e
procedure main()
  integer crsr, price
  sequence text, t
  text = "/GRVY $.75 /GRVY $.75"
  price = 0
  crsr = find('$', text)
  while crsr do
    t = value(text[crsr+1..$])
    if t[1] = GET_SUCCESS then
      price += t[2]*100
    end if
    crsr = find('$', text[crsr+1..$])
  end while
end procedure


If text only equals "/GRVY $.75" then it works fine, but as it is above
it hangs (the debug trace locks up on the end while statement).

Workarounds, anyone?

BTW, I am enjoying using this interpreter on an old DOS box!


------------------------------

This is not really a workaround, but setting the program logic right.

The line
crsr = find('$', text[crsr+1..$])


should read

text = text[crsr+1..$]
	crsr = find('$', text)


This works, as it changes the text so that the loop always appears to start
from scratch. The problem in the original code is that crsr is the position
of the next '$' relative to the previous, but you use it as an absolute
index into the unchanged 'text'.

And since you compute the tail of text twice, that's one time too many:

include get.e
procedure main()
  integer crsr, price
  sequence text, t
  text = "/GRVY $.75 /GRVY $.75"
  price = 0
  crsr = find('$', text)
  while crsr do
    text = text[crsr+1..$]
	t = value(text)
    if t[1] = GET_SUCCESS then
      price += t[2]*100
    end if
    crsr = find('$', text)
  end while
end procedure


Also, if you expect value() to fail sometimes, you probably have to add some
sort of reporting of such a condition, since price, which will be the sum of
all correctly read prices on return, may be inaccurate then. 

Oh, by the way, how do you retrieve the contents of that private variable?
You may need to turn the procedure into a function and add a "return price"
statement after the loop ends. Or to make price local rather than private.

HTH

CChris

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu