home *** CD-ROM | disk | FTP | other *** search
/ Power CD-ROM!! 7 / POWERCD7.ISO / prgmming / clipper / gt_str2a.prg < prev    next >
Text File  |  1993-10-14  |  2KB  |  100 lines

  1. /*
  2.     File......: GT_Str2a.prg
  3.     Author....: Martin Bryant
  4.     BBS.......: The Dark Knight Returns
  5.     Net/Node..: 050/069
  6.     User Name.: Martin Bryant
  7.     Date......: 04/02/93
  8.     Revision..: 1.0
  9.  
  10.     This is an original work by Martin Bryant and is placed
  11.     in the public domain.
  12.  
  13.     Modification history:
  14.     ---------------------
  15.  
  16.     Rev 1.0 04/02/93
  17.     PD Revision.
  18. */
  19.  
  20. /*  $DOC$
  21.  *  $FUNCNAME$
  22.  *      GT_STR2A()
  23.  *  $CATEGORY$
  24.  *      Array
  25.  *  $ONELINER$
  26.  *      Function to convert a string of data to an array.
  27.  *  $SYNTAX$
  28.  *      GT_Str2a(<cString>,<bConvert>],<nLength>) -> aReturn
  29.  *  $ARGUMENTS$
  30.  *      <cString> is a Character string containing fixed
  31.  *      length sub-strings. These sub-strings will be
  32.  *      extracted to be used to form the array.
  33.  *
  34.  *      <bConvert> is the code block which will be passed
  35.  *      the extracted substring and return the converted
  36.  *      data.
  37.  *
  38.  *      <nLength> is the length of each sub-string.
  39.  *  $RETURNS$
  40.  *      GT_Str2a() returns the new array.
  41.  *  $DESCRIPTION$
  42.  *      Function to convert a string of data to an array.
  43.  *      This is generally used to extract data that has
  44.  *      been put into a string for the purposes of storage
  45.  *      to a .mem file. The data can be put into the string
  46.  *      using GT_a2Str().
  47.  *  $EXAMPLES$
  48.  *      // To convert a string of numbers to an array of
  49.  *      // numbers
  50.  *      cNumbers := '112834456378990'
  51.  *
  52.  *      aNumbers := GT_Str2a( ;
  53.  *                      cNumbers, ;
  54.  *                      { | cData | Val(cData) }, ;
  55.  *                      3)
  56.  *
  57.  *      // Giving {112,834,456,378,990}
  58.  *  $SEEALSO$
  59.  *
  60.  *  $INCLUDE$
  61.  *
  62.  *  $END$
  63.  */
  64.  
  65. #include "GT_LIB.ch"
  66.  
  67. FUNCTION gt_str2a(cString,bConvert,nLength)
  68.  
  69. Local aResult  := {}
  70. Local nCount := 0
  71. Local nItems := 0
  72. LOCAL nPos := 1
  73.  
  74. Default cString to ''
  75. Default bConvert to { | | NIL }
  76. Default nLength to 1
  77.  
  78. //  How many items ?
  79. nItems := LEN(cString) / nLength
  80.  
  81. //  Create array
  82. aResult := ARRAY(nItems)
  83.  
  84. //  For each ....
  85. FOR nCount := 1 TO nItems
  86.  
  87.     // Extract
  88.     aResult[nCount] := EVAL(bConvert, ;
  89.         SUBSTR(cString,nPos,nLength))
  90.  
  91.     // Next
  92.     nPos += nLength
  93.  
  94. NEXT
  95.  
  96. /*
  97.     Complete
  98. */
  99. RETURN(aResult)
  100.