About Chandra Archive Proposer Instruments & Calibration Newsletters Data Analysis HelpDesk Calibration Database NASA Archives & Centers Chandra Science Links

Skip the navigation links
Last modified: December 2006

URL: http://cxc.harvard.edu/ciao3.4/extract_element.tm.html
Hardcopy (PDF): A4 | Letter
AHELP for CIAO 3.4 extract_element Context: slangrtl

Synopsis

Extract the nth element of a string with delimiters

Syntax

String_Type extract_element (String_Type list, Integer_Type nth,
Integer_Type delim);

Description

The extract_element function may be used to extract the nth element of the delim delimited list of strings list. The function will return the nth element of the list, unless nth specifies more elements than the list contains, in which case NULL will be returned. Elements in the list are numbered from 0.

Example

The expression

     extract_element ("element 0, element 1, element 2", 1, ',')

returns the string " element 1", whereas

     extract_element ("element 0, element 1, element 2", 1, ' ')

returns "0,".

The following function may be used to compute the number of elements in the list:

     define num_elements (list, delim)
     {
        variable nth = 0;
        while (NULL != extract_element (list, nth, delim))
          nth++;
        return nth;
     }

Alternatively, the strchop function may be more useful. In fact, extract_element may be expressed in terms of the function strchop as

    define extract_element (list, nth, delim)
    {
       list = strchop(list, delim, 0);
       if (nth >= length (list))
         return NULL;
       else
         return list[nth];
    }

and the num_elements function used above may be recoded more simply as:

    define num_elements (list, delim)
    {
       return length (strchop (length, delim, 0));
    }
Hardcopy (PDF): A4 | Letter
Last modified: December 2006



The Chandra X-Ray Center (CXC) is operated for NASA by the Smithsonian Astrophysical Observatory.
60 Garden Street, Cambridge, MA 02138 USA.    Email: cxcweb@head.cfa.harvard.edu
Smithsonian Institution, Copyright © 1998-2004. All rights reserved.