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

Re: David: for your book ?



In article <399937D2.8C0A52A2@dkrz.de>,
  Martin Schultz <martin.schultz@dkrz.de> wrote:
>
> What would you expect to get out of the following:
>
> test = [ 'dir1/', 'directory2/', 'another_directory/' ]
> print,strmid(test,0,strlen(test)-1)
>
> ??
>
> Well, I at least had expected to get the directory names without the
> trailing '/'.
> But, no!
>   help, strmid(test,0,strlen(test)-1)
> reveals that the result is a 3x3 array!!

When IDL added vector capabilities to STRMID in V5.3, they allowed for
both (1) the extraction of  multiple substrings from each element of a
string, and (2) having the first character position and substring length
differ for each element of a string array.    Unfortunately, adding all
this power has made the syntax awkward.   According to the documentation

"If First_Character or Length is an array, the size of their first
dimension determines how many substrings are extracted from each element
of <expression>"

So the solution is to REFORM the string length (or position) array to
two dimensions:

IDL> test = [ 'dir1/', 'directory2/', 'another_directory/']
IDL> print,strmid(test,0,reform( strlen(test)-1, 1, 3) )
===>dir1 directory2 another_directory

By reforming the string length array into a 1 x 3 size, STRMID now knows
to extract only 1 substring from each element of test.

A simple program to extract a string array up to a specified character
using the vector capabilities of STRMID is available at

http://idlastro.gsfc.nasa.gov/ftp/v53/gettok.pro

--Wayne Landsman                          landsman@mpb.gsfc.nasa.gov


Sent via Deja.com http://www.deja.com/
Before you buy.