Re: Concatenate elements of string array [message #16635] |
Wed, 11 August 1999 00:00 |
Ruhaltinger Norbert
Messages: 2 Registered: August 1996
|
Junior Member |
|
|
kluegel@lanl.gov wrote:
>
> Does anyone out there have a clever idea on how to
> concatenate all elements of a one dimensional array
> of strings? Of course, explicit looping is the
> obvious way to do it, but approaches using array
> manipulation tend to be more efficient. Any
> array-based approaches out there?
>
> Thanks for any ideas.
>
string can be used like sprintf in C
You have to create the Format specification on the fly
IDL> string_array=['one ','string ','split ',$
'into ','many ','array elements']
IDL> help,string_array & print,string_array
STRING_ARRAY STRING = Array[6]
one string split into many array elements
IDL> one_string = string(string_array, $
format = '(' + string(n_elements(string_array)) + 'A)')
IDL> help,one_string & print,one_string
ONE_STRING STRING = 'one string split into many array elements'
one string split into many array elements
--
to reply, change the 'SIG' at the end of my adress to 'at' - thx!
|
|
|
Re: Concatenate elements of string array [message #16643 is a reply to message #16635] |
Tue, 10 August 1999 00:00  |
kluegel
Messages: 11 Registered: February 1999
|
Junior Member |
|
|
In article <7oo5ds$c9c@post.gsfc.nasa.gov>,
fireman@mcst.gsfc.nasa.gov (Gwyn Fireman) wrote:
> IDL> string_array=['one ','string ','split ','into ','many ','array
elements']
> IDL> help,string_array & print,string_array
> STRING_ARRAY STRING = Array[6]
> one string split into many array elements
> IDL> byte_array=byte(string_array)
> IDL> byte_array=reform(byte_array,n_elements(byte_array))
> IDL> byte_array=byte_array[where(byte_array ne 0)]
> IDL> one_string=string(byte_array)
> IDL> help,one_string & print,one_string
> ONE_STRING STRING = 'one string split into many array
elements'
> one string split into many array elements
Clever. Reminiscent of APL.
Playing with your approach, I see the BYTE(string_array) produces a 2D
array where one dimension is the number of characters in the longest
string of the original 1D array. So a nasty case would be an array of
strings where one string was very long and all the others were short.
This would make the intermediate 2D array very big.
Another limitation is that we must make a rule that no strings may
contain any nul characters.
Something tells me this is all the better that can be done with
array-based approaches. But just in case... Any other ideas out there?
-- Tom Kluegel
Sent via Deja.com http://www.deja.com/
Share what you know. Learn what you don't.
|
|
|
Re: Concatenate elements of string array [message #16647 is a reply to message #16643] |
Tue, 10 August 1999 00:00  |
fireman
Messages: 49 Registered: August 1991
|
Member |
|
|
kluegel@lanl.gov wrote:
: Does anyone out there have a clever idea on how to
: concatenate all elements of a one dimensional array
: of strings?
I don't know how clever this is, but it worked for me:
IDL> string_array=['one ','string ','split ','into ','many ','array elements']
IDL> help,string_array & print,string_array
STRING_ARRAY STRING = Array[6]
one string split into many array elements
IDL> byte_array=byte(string_array)
IDL> byte_array=reform(byte_array,n_elements(byte_array))
IDL> byte_array=byte_array[where(byte_array ne 0)]
IDL> one_string=string(byte_array)
IDL> help,one_string & print,one_string
ONE_STRING STRING = 'one string split into many array elements'
one string split into many array elements
PLEASE would someone outside of gsfc let me know if you see this!
I've been having all sorts of trouble posting lately.
Tia.
--
-- Gwyn F. Fireman
-- General Sciences Corporation / MODIS Characterization Support Team
-- Gwyn.Fireman@gsfc.nasa.gov 301-352-2118
|
|
|