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

Re: How to convert (concatenate) a string array to a string scalar?



David Fanning wrote:

> Kristian Kjaer (kristian.kjaer@risoe.dk) writes:
> 
> > Given
> > numarray=[1342,1347,1355]  ;, say, I wan't
> > strscalar='1342,1347,1355' ;, but
> > help,string(numarray)
> > gives: STRING = Array[3]
> > and
> > help,total(string(numarray))
> > gives an error.
> >
> > How to convert (concatenate) a string array to a string scalar?
> 
> Now here (I feel pretty darn confident) is the perfect
> opportunity to use a loop in IDL. :-)

BEEEEEP! There's no need for that, here's a function which does what
Kristian wants:

function strconcat, numarray
  format = string( '(', n_elements( numarray ), '(I0,', '","))' )
  str = string( numarray, format=format )
  return, strmid( str, 0, strlen( str ) - 1 )
end

In the example above, the format string which is created inside the
function would be '3(I0,",")' , which tells the STRING routine to take
three integers and add them to the string without leading blanks, but
with a comma between them. The last strmid call only removes the last
comma from the string.

        Alex
-- 
  Alex Schuster     Wonko@weird.cologne.de          PGP Key available
                    alex@pet.mpin-koeln.mpg.de