StrArray Class

From SysCAD Documentation
Jump to navigation Jump to search

Navigation: PGMs ➔ Classes ➔ StrArray Class

SysCAD Pre-Defined Classes and Examples EXAMPLES: User Defined Classes
Sp Databse
Class
PSD
Class
Array
Class
StrArray
Class
Matrix
Class
Tag Select
Class
Plant Model
Class
Noise
Class
Time
Class
Agitator
Power
Reaction
Finder
Aq Feed Conc
Calculator
Evaporation
Correlation
Bayer Liquor
Class
Check
Element Bal

Latest SysCAD Version: 21 September 2021 - SysCAD 9.3 Build 139.29552


Description

The String Array class allows user to Get / Set String values. Please see Array Class for getting/setting numeric values.

The StrArray (String Array) class provides member functions to create a dynamic string array and manipulate its elements.

  • The StrArray is zero indexed, i.e. the index of the first element in the StrArray is zero, and NOT 1. Therefore, the final element in a StrArray of length n will have an index = n-1.
  • If the index is out of range (length <= index) then a " PGM class execution error " will occur and the StrArray operation will be ignored.

Note: the initial length of the array is zero.

Data Members

None

Member Functions

Call Functionality Return Type Parameters Example

SetLen (length)

The length of the StrArray is set to the size of length

length : data type INTEGER, the required length of the StrArray.

a.SetLen(5)

GetLen( )

The function returns the length of the StrArray.

Long

None

x = TagGroup.GetLen()

SetAt (index, Text String)

This sets the element of the array, which corresponds to the given index to the specified Text String.

index : data type INTEGER. The index of the required entry in the StrArray (index starts at zero).

Text String : data type STRING. The value to which the specified element in the StrArray will be set.

TagGroup.SetAt(0, "Variable Name")

GetAt ( index )

This retrieves the value (type real) of the element from the array, which corresponds to the given index.

String

index : data type INTEGER, the index of the required entry in the array (index starts at zero).

Required_Tag = TagGroup.GetAt(0)

SetAll (Text String)

This sets all the elements of the StrArray to the specified Text String.

Text String : data type STRING.

TagGroup.SetAll("Variable Name")

Load(strArrayFileName)

Available in latest version
of Build138 and later.

This creates a new StrArray from a file.

  • The first element on each line, of the specified file, is read in as an element of the array, until either an empty line or end-of-file character is reached.
  • The function returns the value, True if the file was read or, False if the file was not read.

Note: if any elements exist in the StrArray, prior to calling Load, these elements will be overridden.

Bit

strArrayFileName: either the actual filename and path in quotations or a STR variable, which refers to the file.

  • If no path is specified, then it is assume the file is stored in the project folder.
  • Use $Prj in the folder string for folders relative to the project folder.

ArrayFileName = "c:\SysCAD\StrArray.csv"
OK = b.Load(strArrayFileName)
OR
OK = c.Load("$Prj\Controls\InputTags.txt")

Save(strArrayFileName)

Available in latest version
of Build138 and later.

This saves the StrArray data to a comma separated text file.

  • If the specified file exists it is overwritten.
  • The function returns, True if the file was saved or, False if the file was not saved.
  • This could fail for any file access reason, for example: invalid folder, invalid filename, file access error, file already open in another application, etc.

Bit

strArrayFileName: either the actual filename and path in quotations or a STR variable, which refers to the file.

  • If no path is specified, then it is the file is to be saved to the project folder.
  • Use $Prj in the folder string for folders relative to the project folder.

OK = a.Save("OutputTags.txt")
OR
OK = a.Save("$Prj\Controls\OutputTags.txt")

Watch

To make the StrArray variables visible in the access window the Watch command can be used.

  • Individual elements in the array can be made visible - e.g. "Watch TagGroup[2]" or "Watch TagGroup@[2]".
  • A range of StrArray elements can be made visible - e.g. "Watch TagGroup[All,5]" which would show the first 5 elements of the StrArray.
  • If the "@" symbol is included, then the StrArray elements will be read only fields in the access window.

Caution

Using a GetAt within a SetAt can sometimes fail.

;For example:
TagGroup.SetAt(i, TagGroup.GetAt(i+1))  ;This can sometimes fail

;The solution is to use a temporary variable to first retrieve the GetAt value and then call SetAt as follows:
STRING   TmpString
TmpString = TagGroup.GetAt(i+1)
TagGroup.SetAt(i, TmpString)

Example

PageLabel("StrArray")	
  TextLabel()
  STRING     Tag1@, Tag2@, Tag3@, Required_Tag@
  LONG       x@
  StrARRAY   TagGroup
  Watch TagGroup[All,5]

  Sub InitialiseSolution()
    TagGroup.SetLen(5)             ;sets the StrArray length
  EndSub
 
  Tag1 = "P_4.To (C)"
  Tag2 = "P_5.To (C)"
  Tag3 = "P_6.To (C)"

  x = TagGroup.GetLen()
  TagGroup.SetAt(0, Tag1)
  TagGroup.SetAt(1, Tag2)
  TagGroup.SetAt(2, Tag3)
  TagGroup.SetAt(4, "P_7.To (C)")
  Required_Tag= TagGroup.GetAt(1)

See also Example PGM to create a Unit Tag List