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

Re: st: extended function: piece


From   Adrian.P.Mander@gsk.com
To   statalist@hsphsun2.harvard.edu
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.

cheers
Ade
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


                                                                                                                                               
                      khigbee@stata.com                                                                                                        
                                                                                                                                               
                      Sent by:                                                                                                                 
                      owner-statalist@hsphsun2.        To:      statalist@hsphsun2.harvard.edu                                                 
                      harvard.edu                                                                                                              
                                                       cc:                                                                                     
                                                       Subject: Re: st: extended function: piece                                               
                      09-Dec-2003 15:34                                                                                                        
                      Please respond to                                                                                                        
                      statalist@hsphsun2.harvar                                                                                                
                      d.edu                                                                                                                    
                                                                                                                                               
                                                                                                                                               



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/







*
*   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