Re: converting a vector into a string [message #45900] |
Thu, 20 October 2005 15:27 |
Mark Hadfield
Messages: 783 Registered: May 1995
|
Senior Member |
|
|
R.G. Stockwell wrote:
> "Francois L." <fleduc@lycos.com> wrote in message
> news:1129837956.424479@news.drenet.dnd.ca...
>> 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.
>>
>
>
> IDL> help,long(strcompress(strjoin(string([1,2,3,4])),/rem))
>
> <Expression> LONG = 1234
Cool. I was not aware of STRCOMPRESS. It seems it's been in IDL since
the beginning, whereas I've only been using IDL since 1992!
Looking again at the original poster's last sentence, I wonder if
conversion to and from strings isn't an unnecessary detour. He is
starting with integer array [1,2,3,4] and wants to end up with decimal
integer 1234. So perhaps his problem can be interpreted as "convert a
vector of integers (all presumably between 0 and 9) to a number on the
assumption that the elements of the vector represent the digits of the
number in decimal form". If so I suggest
IDL> a = [1,2,3,4]
IDL> help, total(a*10^reverse(lindgen(n_elements(a))), /INTEGER)
<Expression> LONG64 = 1234
--
Mark Hadfield "Kei puwaha te tai nei, Hoea tahi tatou"
m.hadfield@niwa.co.nz
National Institute for Water and Atmospheric Research (NIWA)
|
|
|
Re: converting a vector into a string [message #45903 is a reply to message #45900] |
Thu, 20 October 2005 13:39  |
news.qwest.net
Messages: 137 Registered: September 2005
|
Senior Member |
|
|
"Francois L." <fleduc@lycos.com> wrote in message
news:1129837956.424479@news.drenet.dnd.ca...
> 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.
>
> Thank you,
>
> Franois Leduc
IDL> help,long(strcompress(strjoin(string([1,2,3,4])),/rem))
<Expression> LONG = 1234
Cheers,
bob
|
|
|
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)
|
|
|