Stata The Stata listserver
[Date Prev][Date Next][Thread Prev][Thread Next][Date index][Thread index]

Re: st: extended function: piece


From   khigbee@stata.com
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: extended function: piece
Date   Tue, 09 Dec 2003 09:34:42 -0600

Adrian Mander <Adrian.P.Mander@gsk.com> asks:

> Any reason why piece doesn't appear to allow the length of pieces
> to be 1?
> 
> e.g.
> . local subpiece: piece 2 1 of "abcdefghi"
> invalid syntax
> r(198);
> 
> SYNTAX:        piece #piece_number #length_of_pieces of [`]"string"['] [,
> nobreak ]
> 
> <-- no restriction is mentioned on the STATA help system.
> 
> And is piece going to be computationally faster than substr?


Answering last question first:  the -substr()- function will be
faster.  It doesn't have to worry about all the logic that
-piece- goes through to determine appropriate breaks.

Now to the first question.  The -piece- macro extended function
was designed for cases where a programmer is trying to produce
output that is to fit within a certain width and the text is
allowed to wrap, but it would be nice for the text to break to a
new line at word boundaries instead of in the middle of a word if
possible.  Since the advent of -smcl- in Stata, the -piece- macro
function is seldom needed.

-piece- was never meant to be used in a manner similar to
-substr()-.  Even if we changed the code for -piece- to allow the
"#length_of_pieces" to be 1, it may not be what you are really
wanting.  Think in terms of what it does with spaces.

    . local a : piece 2 3 of "abc       d       efg"
    . di "|`a'|"
    |d|

This is a feature of -piece-.  It will treat multiple spaces as
one space and trim off leading and trailing spaces for the
result.  This is most often what is wanted when trying to use
-piece- for producing a (possibly) wrapped text output.

We should update the documentation to indicate that 2 is the
minimal "#length_of_piece" allowed.

If you want to provide more context for the overall task you are
trying to accomplish, maybe someone will be able to suggest a
better approach.

Ken Higbee    khigbee@stata.com
StataCorp     1-800-STATAPC

*
*   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–2014 StataCorp LP   |   Terms of use   |   Privacy   |   Contact us   |   What's new   |   Site index