Pop method: Array class
Syntax
Pop()
Description
The Pop method removes the last element from the array and returns its value.
Parameters
None.
Returns
The value of the last element of the array. If the last element is a subarray, the subarray is returned.
Example
The Pop method can be used with the Push method to use an array as a stack. To put values on the end of the array, use Push. To take the values back off the end of the array, use Pop.
Suppose we have a two-dimensional array &SUBPARTS which gives the subparts of each part of some assemblies. Each row (subarray) of &SUBPARTS starts with the name of the part, and then has the names of the subparts. Assuming there are no "loops" in this data, the following code puts all the subparts, subsubparts, and so on, of the part given by &PNAME into the array &ALLSUBPARTS, in "depth first order" (that is, subpart1, subparts of subpart1, …, subpart2, subparts of subpart2, …). We stack the indexes into &SUBPARTS when we want to go down to the subsubparts of the current subpart.
Local array of array of string &SUBPARTS;
Local array of string &ALLSUBPARTS;
Local array of array of number &STACK;
Local array of number &CUR;
Local string &SUBNAME;
/* Set the ALLSUBPARTS array to an empty array of string. */
&ALLSUBPARTS = CreateArrayRept("dummy", 0);
/* Start with the part name. */
&STACK = CreateArray(CreateArray(&SUBPARTS.Find(&PNAME), 2));
While &STACK.Len > 0
&CUR = &STACK.Pop();
If &CUR[1] <> 0 And
&CUR[2] <= &SUBPARTS[&CUR[1]].Len Then
/* There is a subpart here. Add it. */
&SUBNAME = &SUBPARTS[&CUR[1], &CUR[2]];
&ALLSUBPARTS.Push(&SUBNAME);
/* Tour its fellow subparts later. */
&STACK.Push(CreateArray(&CUR[1], &CUR[2] + 1));
/* Now tour its subsubparts. */
&STACK.Push(CreateArray(&SUBPARTS.Find(&SUBNAME), 2));
End-If;
End-While;