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

Re: st: extended function: piece

Subject   Re: st: extended function: piece
Date   Tue, 9 Dec 2003 17:04:11 +0000

Actually I thought that piece would have been faster and
I was just optimising some old code.

I had some strings that never have any spaces
and I wanted to loop through individual letters quickly.
So now I shall leave it as substr() ...

Logically speaking  -piece- should allow a piece length of 1 but that is up
to you guys at StataCorp.

Adrian Mander MSc PhD, Principal Statistician, GlaxoSmithKline, Mail Code
HW8133, New Frontiers Science Park (South), Harlow, Essex, CM19 5AW. Tel:
01279 63 1203 Fax: 01279 64 4003

                      Sent by:                                                                                                                 
                      owner-statalist@hsphsun2.        To:                                                 
                                                       Subject: Re: st: extended function: piece                                               
                      09-Dec-2003 15:34                                                                                                        
                      Please respond to                                                                                                        

Adrian Mander <> 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'|"

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
StataCorp     1-800-STATAPC

*   For searches and help try:

*   For searches and help try:

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