Re: simple string manipulation problem [message #17521] |
Fri, 15 October 1999 00:00 |
J.D. Smith
Messages: 214 Registered: August 1996
|
Senior Member |
|
|
Jason Brookes wrote:
>
> hi all,
>
> here is a seemingly simple problem:
>
>
> PRO string_test
>
> PRINT,'why does','this happen ?',FORMAT = '(A,/,A)'
> error_message = STRING('why does','this happen ?',FORMAT = '(A,/,A)')
> PRINT,error_message
>
> PRINT,'why does','this happen ?',FORMAT = '(A,5x,A)'
> error_message = STRING('why does','this happen ?',FORMAT = '(A,5x,A)')
>
> PRINT,error_message
>
> END
>
>
> on running this routine, i see the following:
>
> IDL> string_test
> % Compiled module: STRING_TEST.
> why does
> this happen ?
> why does this happen ?
> why does this happen ?
> why does this happen ?
> IDL>
>
> the $64,000 question is ... WHY DOES THIS HAPPEN ? is it a bug,
> ahem, sorry... feature ? i do not understand why the newline character
> is not interpreted when i send the 'error_message' argument to the
> PRINT procedure via the STRING function. As can be seen, the output is
> consistent when the newline character is replaced by 5 space
> characters.
>
>
The reason is that PRINT and STRING are not totally consistent with each
other where formatting is concerned. Print knows it is printing to
something, and can easily insert a newline where it sees '/'. In fact,
in the manual, it says:
*Record Terminators*
q is zero or more slash (/) record terminators. On output, each record
terminator causes the output to move to a new line. On input, each
record terminator causes the next line of input to be read.
That is '/' operates on *output* only, and is likely silently ignored in
the creation of strings.
To get the effect you want, try:
nl=string(10b)
error_message = STRING('why does',nl,'this happen ?',FORMAT = '(3A)')
JD
--
J.D. Smith |*| WORK: (607) 255-5842
Cornell University Dept. of Astronomy |*| (607) 255-6263
304 Space Sciences Bldg. |*| FAX: (607) 255-5875
Ithaca, NY 14853 |*|
|
|
|