Janis Papanagnou wrote:
> Roland Mainz wrote:
> > The following testcase contains a simple "stopwatch" type which tracks
> > the time between the functions "start" and "stop" and maintains a
> > variable "diff" which contains the difference:
> > -- snip --
> > typeset -T stopwatch_t=(
> >       float starttime=nan
> >       float stoptime=nan
> >       float diff=nan
> >
> >       function start
> >       {
> >               (( _.starttime=SECONDS ))
> >               return 0
> >       }
> >       function stop
> >       {
> >               (( _.stoptime=SECONDS ))
> >               return 0
> >       }
> >       function reset
> >       {
> >               (( _.starttime=nan , _.stoptime=nan ))
> >       }
> >       function diff.get
> >       {
> >               (( .sh.value=_.stoptime-_.starttime ))
> >               print -u2 "#mark"
> >               return 0
> >       }
> > )
> >
> > stopwatch_t sw
> >
> > sw.start
> > sleep 2.1
> > sw.stop
> >
> > printf "time=%f\n" sw.diff
> > -- snip --
> >
> > However when I run this testcase with ast-ksh.2009-06-22 it only prints:
> > -- snip --
> > time=nan
> > -- snip --
> > ... while the expected output should AFAIK be:
> > -- snip --
> > #mark
> > time=2.1000
> > -- snip --
> >
> > AFAIK two things go wrong there:
> > 1. Using $ printf "time=%f\n" sw.diff # to call the discipline method
> > stopwatch_t.diff.get does not work, however it works when I replace
> > "sw.diff" with "${sw.diff}"
> > 2. The somehow the value assigned to ".sh.value" does not make it to the
> > "printf" builtin... but I am not sure why this happens...
> 
> Works for me if I omit the '_.' in front of the starttime/stoptime variables
> (and use "${sw.diff}").

The variable "_" is needed like C++ classes need "this" (in some cases),
otherwise you just use global variables instead. And using "${sw.diff}"
works because it converts the value to a string first, however this
triggers a base2-->base10 conversion. The AST printf allows the use of
arithemtric expressions but in this case the discipline method (e.g.
"diff.get") is not being called.

----

Bye,
Roland

-- 
  __ .  . __
 (o.\ \/ /.o) [email protected]
  \__\/\/__/  MPEG specialist, C&&JAVA&&Sun&&Unix programmer
  /O /==\ O\  TEL +49 641 3992797
 (;O/ \/ \O;)
_______________________________________________
ast-users mailing list
[email protected]
https://mailman.research.att.com/mailman/listinfo/ast-users

Reply via email to