Parse to Arrays

This takes a block of field and record delimited text and parses it to arrays of various types. Lets say you have a small tab delimited text file with name, invoice number, invoice date and amount. Read the file then pass it along with pointers to the arrays to ParseToArrays and the arrays will be filled with the proper type of data, almost as if the Keebler Elves had done the work for you.

Here's some example code:
Array Text(aName;0)
Array String (14;aInvoiceNumber;0)
Array Date(aInvoiceDate;0)
Array Real(aInvoiceAmount;0)
$RecordCount:=ParseToArrays($Text;Char(9);Char(13);->aName;->aInvoiceNumber;->aInvoiceDate;->aInvoiceAmount)
(Where $Text is the tab delimited text from a spreadsheet or an import or maybe Santa!)

ParseToArrays calls ParseToArray and ArrayResize which can be found on the previous page.
  ` ParseToArrays
  ` Parses text to multiple arrays
  ` The arrays can be Text, String, Date or Boolean

C_TEXT($Text;$FD;$RD)
C_POINTER($ArrayPtr)
C_LONGINT($ArrayCount;$ArrayNdx;$FirstArrayNdx;$Ndx)

$Text:=$1
$FD:=$2
$RD:=$3
$FirstArrayNdx:=4  ` The first array pointer is the 4th parameter

ARRAY TEXT(aParseToArrays_OuterText;0)
$LineCount:=ParseToArray ($Text;->aParseToArrays_OuterText;$RD)
$ArrayCount:=Count parameters-$FirstArrayNdx+1

For ($Ndx;1;$ArrayCount)  ` Resize all the arrays
  $ArrayNdx:=$Ndx+$FirstArrayNdx-1
  $ArrayPtr:=${$ArrayNdx}
  ArrayResize ($LineCount;$ArrayPtr)
End for 

For ($Line;1;$LineCount)
  ARRAY TEXT(aParseToArrays_InnerText;0)
  $LineText:=aParseToArrays_OuterText{$Line}
  $FieldCount:=ParseToArray ($LineText;->aParseToArrays_InnerText;$FD)
  For ($FieldNdx;1;$FieldCount)
    $ArrayNdx:=$FieldNdx+$FirstArrayNdx-1
    If ($FieldNdx<=$ArrayCount)  ` ($ArrayNdx<=Count parameters)
      $ArrayPtr:=${$ArrayNdx}
      $Type:=Type($ArrayPtr->)
      $Cell:=aParseToArrays_InnerText{$FieldNdx}
      Case of 
        : (($Type=String array) | ($Type=Text array))
          $ArrayPtr->{$Line}:=$Cell
          
        : (($Type=Real array) | ($Type=Integer array))
          $ArrayPtr->{$Line}:=Num($Cell)
          
        : ($Type=LongInt array)
          If ((Position(":";$Cell)>0) | (Position("AM";$Cell)>0) | (Position("PM";$Cell)>0))
            $ArrayPtr->{$Line}:=Time($Cell)
          Else 
            $ArrayPtr->{$Line}:=Num($Cell)
          End if 
          
        : ($Type=Date array)
          $ArrayPtr->{$Line}:=Date($Cell)
          
        : ($Type=Boolean array)
          If (($Cell="T@") | ($Cell="1") | ($Cell="On") | ($Cell="Y@"))
            $Boolean:=True
          Else 
            $Boolean:=False
          End if 
          $ArrayPtr->{$Line}:=$Boolean
        Else 
            ` For the ones that can't be converted (2D; Picture;Pointer)
      End case 
      
    End if 
  End for 
End for 

ARRAY TEXT(aParseToArrays_OuterText;0)
ARRAY TEXT(aParseToArrays_InnerText;0)

$0:=$LineCount