Re: converting a vector into a string [message #45905 is a reply to message #45903] |
Thu, 20 October 2005 13:33  |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
Francois L. wrote:
> Hello,
>
> Sorry, I am reposting this message again because the previous one contained
> many errors in the header...
>
> I have a vector A (integers):
>
>> A = [1,2,3,4]
>
> that I want to convert into a string.
>
> If I use the command
>
>> B = string(A)
>
> then B is a string of four elements.
>
> How can I convert A to string in order to have '1234' ? which is not an
> array of four elements...
>
> Well at the end, I want to convert '1234' into the number 1234.
There are several ways you could do this--it depends on exactly what
you're trying to do--but how about:
IDL> print, strjoin(strtrim(string([1,2,3,4]),2))
1234
Taking the function calls from inside out: STRING converts the integer
array to a string array; STRTRIM trims spaces from each element of the
string array (setting the second argument to 2 ensures that it removes
both leading and trailing spaces); and STRJOIN joins the elements into a
single string.
Actually, the call to STRING is redundant, as STRTRIM does the
conversion automatically, so the following will achieve the same thing.
IDL> print, strjoin(strtrim([1,2,3,4],2))
Alternatively, you could use formatted output, eg:
IDL> print, string([1,2,3,4], FORMAT='(4I)')
1 2 3 4
IDL> print, string([1,2,3,4], FORMAT='(4I0)')
1234
1234
--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|