3.7.5.57 StringArray

The following methods are supported for StringArray type in LabTalk:

Add

Add a string to a StringArray variable.

Syntax

strArray.Add(arg$);

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
type "aa has $(aa.GetSize()) strings in it"; 
//should print aa has 2 strings in it

Append

Append from

  1. Another StringArray.
  2. datasets
  3. A stringarray, which is created from a delimiter-separated string. The default delimiter is a whitespace character (space, tab, newline, etc.). (8.0 SR6)
  4. A treenode. (8.1 SR1)

Syntax

int Append(arg$[, chDelimiter]);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

Example1

StringArray aa;
StringArray bb;
aa.Add("Boston");
aa.Add("New York");

bb.Add("London");
bb.Add("Paris");
aa.Append(bb);

loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//should print:
//Boston
//New York
//London
//Paris

Example2 (If there are two cell values "Rome", "Tokyo" in the first two cell on Col 1 of the active workbook layer.)

StringArray aa;
StringArray bb;
aa.Add("Boston");
aa.Add("New York");

Range c1 = 1;
aa.Append(c1);
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//should print:
//Boston
//New York
//Rome
//Tokyo

Example3 (8.0 SR6)

StringArray sa;
sa.Append("Boston|New York|DC", "|");

loop(ii,1,sa.GetSize())
{
  string str$=sa.GetAt(ii)$;
  str$=;
}
//should print:
//Boston
//New York
//DC

Example4, appending treenode: (8.1 SR1)

newbook;
string fn$=system.path.program$ + "\samples\statistics\automobile.dat"; 
impasc fname:=fn$;
tree mytree;
// Perform descriptive statistics
discfreqs 2 rd:=mytree;
// Append result to stringarray
StringArray sa;
sa.append(mytree.freqcount1.data1);
ty "There are $(sa.getSize()) Makes";
// Should print: 
// There are 18 Makes

Copy

Copy the source StringArray.

Syntax

int Copy(StringArray);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
StringArray bb;
if(bb.Copy(aa)==0)
  type "Done!";
else
  type "Error!";
loop(ii,1,bb.GetSize())
{
  string str$=bb.GetAt(ii)$;
  str$=;
} 
//Should print:
//Done!
//Boston
//New York

CopyFrom

(8.0 SR5) Copy the contents of a dataset to this string array.

Syntax

int CopyFrom(range[,TranslateLink]);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
if(aa.CopyFrom(col(a))==0)
{
  aa.Sort();
  aa.CopyTo(col(B));
}
else
  type "Error!";

CopyTo

Copy the StringArray to a dataset.

Syntax

int CopyTo(range[, CheckNumeric]);

(80 SR5) CheckNumeric is an optional argument (default = false) to allow numerics to be set into cells as numeric cells. If false, even numeric will be set into cells as text.

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
aa.Add("123");
Range c1 = 1; // first column of active sheet
if(aa.CopyTo(c1)==0)
  type "Done!";
else
  type "Error!";
//With workbook active, should print Done. 
//Boston,New York and 123 should show in first column 
//and even the "123" cell should be justified left as Text cells

To see how CheckNumeric is used, try the following:

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
aa.Add("123");
aa.CopyTo(col(2),2,1);//starting from row2 and have 123 added 
//as numeric to be justified right
aa.CopyTo(col(1));// as a comparison to use default to start from 1st row
//and even numeric will become text

Find

Find a string in the array.

Syntax

int nInd = strArray.Find( arg$ );

Returns

Return index (1 offset) if found, otherwise return 0 if not in array.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");

int nn = aa.Find("Boston");
nn=; // should print out 1

string str$="Washington DC";
aa.Add(str$);

nn = aa.Find(str$);
nn =; // should print 3
nn = aa.Find("Chicago");
nn =; // should print 0

InsertAt

Insert the splicify string at the given index(based - 1) in the StringArray.

Syntax

int InsertAt(ind, str$);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
if(aa.InsertAt(1, "Washton DC")==0)
  type "Done!";
else
  type "Error!";
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//Should print:
//Done!
//Washton DC
//Boston
//New York

GetAt

Get the string at the given index.

Syntax

string str = strArray.GetAt(arg$)$

Returns

If index >=1 and index <= StringArray size, return the given index string in the StringArray, else return empty string.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
aa.Add("Washington DC");
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//should print:
//Boston
//New York
//Washington DC

SetAt

Set the string at the given index in StringArray. If the given index ind >=0 and ind < StringArray size, it will set the string to the StringArray at the given index. If not, it will do nothing.

Syntax

strArray.SetAt(nInd, arg$);

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
aa.Add("Washington DC");
aa.SetAt(2, "Chicago");
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//should print:
//Boston
//Chicago
//Washington DC

SetAtGrow

Set the string at the given index in StringArray. If the given index ind >=0 and ind < StringArray size, it will set the string to the StringArray at the given index. If ind > StringArray size, it will set the string at the given index and enlarge the StringArray.

Syntax

int SetAtGrow(index, str$);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
string str2$ = "Los Angeles";
if(aa.SetAtGrow(4, str2$)==0)
  type "Done!";
else
  type "Error!";
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//Should print:
//Done!
//Boston
//New York
//
//Los Angeles

GetSize

Get the StringArray size.

Syntax

int nRet = strArray.GetSize();

Returns

Return the StringArray size. If Empty, return 0;

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
int nn = aa.GetSize();
nn=; //should return 2;
aa.Add("Washington DC");
nn=aa.GetSize(); //should return 3;
nn=;

SetSize

Set the StringArray size. You will need to call this to prepare the array before you can access it unless you are using Add or Append.

Syntax

strArray.SetSize(nSize);

Examples

StringArray aa;
aa.SetSize(2);
aa.SetAt(1,"Boston");
aa.SetAt(2,"New York");
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//should print:
//Boston
//New York

RemoveAt

Remove the given index(based - 1) string in the StringArray.

Syntax

int RemoveAt(index);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
if(aa.RemoveAt(1)==0)
  type "Done!";
else
  type "Error!";
loop(ii,1,aa.GetSize())
{
  string str$=aa.GetAt(ii)$;
  str$=;
} 
//Should print:
//Done!
//New York

Sort

(80 SR5)Sort this string array.

Syntax

int Sort(Descending=0, IgnoreCase=0);

Returns

Returns 0 if success, otherwise return negative error codes.

Examples

StringArray aa;
aa.Add("Boston");
aa.Add("New York");
aa.Add("boston");
aa.Add("new york");
StringArray bb;
bb.Copy(aa);
bb.Sort();//default is ascending and case sensitive
bb.CopyTo(col(A));
bb.Copy(aa);
bb.Sort(1, 1);
bb.CopyTo(col(B));