Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Error in strdup

    Dear All,

    I am trying to understand the exact reason the string duplication fails sometimes.
    The documentation illustrates some possible uses:
    Click image for larger version

Name:	strdup.png
Views:	1
Size:	31.8 KB
ID:	1527219


    Yet, when I run this (highlighted) example in Stata I am getting an error message:
    Click image for larger version

Name:	strdup2.png
Views:	1
Size:	5.5 KB
ID:	1527220


    For the moment, I've noticed that enclosing the string duplication in parenthesis helps and can be used as a workaround.
    I am also not sure whether the order matters in Num*String or String*Num, since Num*String seems to work fine.

    The above is from Stata MP 16.0 (Windows 64-bit), but the same was earlier observed in Stata 15.1.

    Thank you for any comments on the above behavior,
    Sergiy




  • #2
    The problem is not in duplicating strings, but rather with the display command, which is notorious for trying - and sometimes failing - to identify expressions and replace them with their evaluated values. Here are three more examples along the lines of yours.
    Code:
    . display `""hello"*3"'
    "hello"*3
    
    . display `="hello"*3'
    hellohellohello not found
    r(111);
    
    . display "`="hello"*3'"
    hellohellohello
    When you remove the display command from the process, you can see that the string duplication works as documented when the expression is parsed as intended.
    Code:
    . local s1 = 3*"hello"
    
    . local s2 = "hello"*3
    
    . macro list _s1 _s2
    _s1:            hellohellohello
    _s2:            hellohellohello

    Comment


    • #3
      William, thank you for your thoughts.

      Regarding the examples, I don't have any problem with the 1st and the 3rd examples you provided.

      Code:
      . display `""hello"*3"'
      "hello"*3
      
      . display "`="hello"*3'"
      hellohellohello
      I might agree to #2 as well, seeing how #3 works. Yet I don't get the difference between expressions 3*"A" and "A"*3 as seen by the display command. I have tried to find other examples, but can't, e.g. list command does not evaluate the expression 3*"A" and complains whichever way it is written, yet the if command is always evaluating the expression correctly, again, regardless which way it is written. I don't see why display is anything special here. For me it is a bug 🐛 in the display command then.

      Thank you, Sergiy

      Comment


      • #4
        A close reading of [R] display tells us two of the many allowed display directives are
        "double-quoted string"
        [%fmt] [=] exp
        Your problem can be solved by being explicit to display that you are presenting an expression to evaluate rather than a double-quoted string followed by something else.
        Code:
        . display "hello"*3
        hello*3 invalid name
        r(198);
        
        . display = "hello"*3
        hellohellohello
        While I am not fond of the display command its problems arise from attempting to infer more than it can do successfully, rather than from a bug. It should have required the "=" before an expression, but that would have made the interactive use cumbersome to accommodate the very small percentage of edge cases. In your case, display treats "hello" as a string to display (note that it is not in red) and since the next tokens are not within a string, guesses that they form an expression to be evaluated, and throws an error in evaluating this expression, consistent with the error thrown by
        Code:
        . local s = * 3
        *3 invalid name
        r(198);
        Statalist is rife with examples of display presenting unexpected results. My own preference as a programmer is to evaluate expressions into local macros and then present the results within a quoted string that includes the dereference of the local macro. That way, display isn't trying to infer what is wanted.

        Comment

        Working...
        X