Statalist The Stata Listserver


[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: Reproducable system freeze on displaying certain output in Mata


From   Alan Riley <[email protected]>
To   [email protected]
Subject   Re: st: Reproducable system freeze on displaying certain output in Mata
Date   Wed, 13 Jun 2007 17:54:54 -0500

David Elliott ([email protected]) reported a bug related to the
handling of newline characters in Mata.  His original email can be
found beneath my signature for reference.

David's bug is reproducible, and we will fix it in a future
executable update.

For programmers interested in technical details and a workaround,
continue reading.

David wrote that sprintf() in Mata converts "\n" to 0a.  What David
is seeing is the hexidecimal representation of \n.  \n, or newline,
is represented as character 10 in decimal, which is character 0a
in hexidecimal.  Mata should be able to handle embedded 0a characters
in the middle of a string, but it has a bug which we will fix in
the next executable update.

David can work around this bug by changing the hex character 0a
back to the literal string characters '\' and 'n' ("\n") in his
string.  Using his example, just after he checks the final contents of
'line', he can use Mata's subinstr() function to replace 0a (ascii
character 10) with \n:

   : line
    {txt}the result is {res}5.213 0a{txt}let's add another line 0a

   : line = subinstr(line, char(10), "\n")

   : line
    {txt}the result is {res}5.213 \n{txt}let's add another line \n

   : printf(line)
   the result is 5.213 
   let's add another line 


--Alan
([email protected])


David's original email:
> I'm having an issue with displaying output in Mata.  I am essentially
> trying to use sprintf() to accumulate an output string in a string
> scalar and then display it with a single display() or printf().  My
> difficulty arises when there is more than one newline character "\n"
> which sprintf() converts to "0a" in the accumulated string.
> 
> The following illustrates the problem using an interactive session
> with the following commands and output:
> 
> mata
> : result = 5.213
> 
> : sprintf("{txt}the result is {res}%f \n",result)
>  {txt}the result is {res}5.213 0a
> 
> : line = sprintf("{txt}the result is {res}%f \n",result)
> 
> : line
>  {txt}the result is {res}5.213 0a
> 
> : printf(line)
> the result is 5.213
> 
> : display(line)
> the result is 5.213
> 
> : line = line + sprintf("{txt}let's add another line \n")
> 
> : line
>  {txt}the result is {res}5.213 0a{txt}let's add another line 0a
> 
> [warning! the next line may freeze Stata]
> : printf(line)
> the result is 5.213
> 
> screenful of blank lines ...
> 
> --more--
> 
> At this point the more condition cannot be cleared and neither a
> keyboard ctrl+break nor a GUI break have any effect and the processor
> utilization rises to 50% indicating some form of infinite loop
> condition. The program cannot be terminated from the GUI and must be
> shutdown using the Task Manager. Using -display(line)- causes the same
> condition.
> 
> 1) Am I misusing sprintf() printf() or display()?
> 2) Is it appropriate to build a long display string as I have attempted.
> 3) If not, can someone else duplicate this behaviour?
> 4) Is there a workaround?
> 
> System info:
> Stata/SE 9.2 running on XP Pro SP2 with 316,685K memory alloted out of
> 1Gb system memory.
<end>
*
*   For searches and help try:
*   http://www.stata.com/support/faqs/res/findit.html
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/



© Copyright 1996–2024 StataCorp LLC   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index