Development‎ > ‎VBScript‎ > ‎

Serializing and deserializing two dimentional array

Arrays are a great scripting tool used as the data containers.

Following code converts any two dimentional array into XML document. It can be then saved into text file and restored using deserialization function.

'*********************************************************************************
'* Function signature:
'* Description:
'*                  
'*
'* Parameters
'*
'*
'*
'* Return Value
'*
'*********************************************************************************
Function array_Serialize(aArray)
      Dim sSerializedString
     
      For iRow = 0 to UBound(aArray, 1)
            For iColumn = 0 to UBound(aArray, 2)
                  sSerializedString = sSerializedString & aArray(iRow, iColumn) & "<FS>"
            Next
 
            sSerializedString = left(sSerializedString, len(sSerializedString)-4)
 
            sSerializedString = sSerializedString & "<RS>"
    Next
 
      sSerializedString = left(sSerializedString, len(sSerializedString)-4)
      SerializeArray = sSerializedString
 
End Function


Following code reverses above code and read XML file into two dimentional array.

'*********************************************************************************
'* Function signature:
'* Description:
'*                  
'*
'* Parameters
'*
'*
'*
'* Return Value
'*
'*********************************************************************************
Function array_Deserialize(sSerializedString, byRef aArray)
 
      Dim iRowIndex, iColumnIndex
 
      For each sRecord in split(sSerializedString, "<RS>")
            For each sField in split(sRecord, "<FS>")
                  aArray(iRowIndex, iColumnIndex) = sField
                  iColumnIndex = iColumnIndex + 1
            Next
            iRowIndex = iRowIndex + 1
            iColumnIndex = 0
      Next
 
End Function

Comments