Statalist


[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: st: Length for strings, ignoring SMCL tags


From   Jeph Herrin <junk@spandrel.net>
To   statalist@hsphsun2.harvard.edu
Subject   Re: st: Length for strings, ignoring SMCL tags
Date   Wed, 03 Dec 2008 11:35:13 -0500



Well, for a moment there I thought that mata's -sprintf()- was
the solution, because it does what -printf()- does, but returning
a string. Only, further down there's a qualifier, that it doesn't
parse SMCL (which -printf()- does). Seems like the natural place
to add this functionality, if StataCorp decides to.

cheers,
Jeph


Brian Karfunkel wrote:
Thanks for the help so far, it's at least nice to know I'm not
overlooking something that's easy.

I'm trying to output a series of locals in a nice tabular format, with
pipes (i.e. "|") between columns and a line of hyphens separating
rows. So, I iterate through a list of rows and columns, etc., and
ultimately have a string that contains the text for each row. For a
2X2 with Hello, World, Longval, and a blank as values that would be
something like:

local row1 "| Hello{col 10} | World{col 18} |"
local row2 "| Longval{col 10} | {col 18} |"

which outputs to:

| Hello   | World |
| Longval |       |
(note that in fixed-char font the column dividers line up).

The point is, the width of each column is set by the longest value in
that column. However, if, say, only one value in a column is very
long, I don't want to have that whole column be very wide, but rather
want something like this as output:

--------------------
| Test | Table |
--------------------
| tst2 | Table |
-------------------------------------------------------------
| Really abnormally long cell value | Table |
-------------------------------------------------------------
| Test | Table |
--------------------

The problem is getting the right number of hyphens to be displayed; I
want as many hyphens in the row-separators as there are characters in
the output row, which is just the length of the row as it is
displayed. I can work around it by keeping track of the length of each
component of the output row (e.g. "Test" is 4, " | " is 3, "Table" is
5, etc.), but I was hoping there was a less cumbersome way, though it
doesn't seem that there is.

Anyway, thanks again for the help.

-Brian

On Tue, Dec 2, 2008 at 1:22 PM, Nick Cox <n.j.cox@durham.ac.uk> wrote:
My diagnosis is the same. Or at least, I battered my head against this
and could think of nothing that suited.

However, let's go one step back. Why do you want this?

Nick
n.j.cox@durham.ac.uk

Austin Nichols

Brian Karfunkel <bkarfunkel@gmail.com>:
According to -help extended_fcn- you can't use display directives with
"in" and "as":

display in smcl "{err}hello"
loc g: display in smcl "{err}hello"
di strlen(`"`:display in smcl "{err}hello"'"')

which is unfortunate for your particular example. So you are stuck
removing all {smcl} before computing length--and during that process
counting spaces if you want them.

On Tue, Dec 2, 2008 at 3:58 PM, Brian Karfunkel <bkarfunkel@gmail.com>
wrote:

Is there a way to get the length of a string while ignoring any SMCL
tags in the string?

In other words, I have "{err}hello", and I want the length of "hello",
since the "{err}" tag doesn't affect the length as outputted.

I've tried the following:

strlen(`"`: display "{err}hello"'"')

and variations thereof, but nothing seems to work. If there is a
solution, it would also be helpful to know how it would treat "{col
30}hello" and "{space 20}hello", for example, in terms of whether the
columns/spaces that are inserted are counted in the length (i.e.
whether it is literally the number of characters of the line on the
screen as displayed or if it is just the length of the string you'd
get if you, say, -regexr-ed out all the tags in braces).
*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   http://www.stata.com/support/statalist/faq
*   http://www.ats.ucla.edu/stat/stata/




*
*   For searches and help try:
*   http://www.stata.com/help.cgi?search
*   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