Chapter Contents

Previous

Next
POPC, POPL, POPN, and POPO

POPC, POPL, POPN, and POPO



Removes an item from an SCL list and returns the value of the item

Category: List


Syntax
Details
Examples
Example 1: Using the POPC Function
Example 2: Using the POPL Function
Example 3: Using the POPN Function
Example 4: Using the POPO Function
See Also

Syntax

cval=POPC(list-id<,index>);
sublist-id=POPL(list-id<,index>);
nval=POPN(list-id<,index>);
obj-id=POPO(list-id<,index>);

cval
contains the character value that was removed from the list by POPC.

Type: Character

sublist-id
contains the identifier of the sublist that was removed from the list by POPL.

Type: List

nval
contains the numeric value that was removed from the list by POPN.

Type: Numeric

obj-id
contains the identifier of the object item that was removed from the list by POPO.

Type: Object

list-id
contains the identifier of the list from which the value or sublist is removed. An invalid list-id produces an error condition.

Type: List

index
is the position of the item in the list. The position can be specified as a positive or negative number. By default, index is 1 (the first item). If index is a positive number, then the item is at position index from the beginning of the list. If index is a negative number, then the item is at position ABS(index) from the end of the list. An error condition results if the absolute value for index is zero or if it is greater than the number of items in the list.

Type: Numeric or List


Details

These functions are useful for implementing stacks and queues of values.

An error condition results if

To check the attributes of a list or list item, use HASATTR. To change attributes, use SETLATTR. Use ITEMTYPE to test the type of an item when the list contains items with types other than the one for which you are searching.


Examples

These examples assume that all list items are character. Programs A and B are equivalent. Both remove the last item in an SCL list.

A:  cval1=popc(listid,-1);
    put cval1=;
B:  cval2=getitemc(listid,-1);
    put cval2=;
    listid=delitem(listid,-1);

Example 1: Using the POPC Function

This example creates an SCL list called TODOQ, which represents a queue of tasks to do. A SAS/AF FRAME entry has a text entry named NEWTASK for entering a new task into the TODOQ queue. A second text entry, TODO, displays the first task in the to-do queue. A DONE button removes the top task from the TODOQ queue.

INIT:
   todoq = makelist();
   done._gray();
return;

NEWTASK:
   todoq = insertc(todoq, newtask.text, -1);
      /* Enqueue */
   newtask.text = '';
   cursor newtask;
return;

DONE:
       /* Dequeue */
   finished = popc(todoq);
return;

MAIN:
   if listlen(todoq) then do;
      done._ungray();
      todo.text = getitemc(todoq);
   end;
   else do;
      done._gray();
      todo.text = '';
   end;
return;

TERM:
   rc = dellist(todoq);
return;

Example 2: Using the POPL Function

This program searches for, retrieves, and deletes the first sublist item from the list LISTID:

LOOP:
   do i=1 to listlen(listid);
      if itemtype(listid,i)='L' then
         do;
            list=popl(listid,i);
            leave loop;
         end;
   end;
   ...other SCL statements...

Example 3: Using the POPN Function

This example creates a new list called DATETIMES and treats it as a stack. The entry displays SAS datetime values when the button PUSH is pressed. The text entry pops and displays SAS datetime values from the DATETIMES stack when the button POP is pressed.

INIT:
   datetimes = makelist();
   pop._gray();
return;

PUSH:
   datetime.value = datetime();
   datetimes = insertn(datetimes, datetime.value);
   pop._ungray();
return;

POP:
   datetime.value = popn(datetimes);
   if listlen(datetimes) = 0 then
      pop._gray();
return;

TERM:
   rc = dellist(datetimes);
return;

Example 4: Using the POPO Function

This example inserts an object into an SCL list and then removes the object from the list:

DCL sashelp.fsp.object.class obj1;
DCL object obj2;
init:
   obj1=_new_ sashelp.fsp.object.class();
   l1 = makelist();
   l1 = inserto(l1,obj1);
     /* Insert object as first item of list */
   obj2 = popo(l1);
     /* Remove object from the list into obj2 */
return;

See Also

DELITEM

GETITEMC, GETITEML, GETITEMN, and GETITEMO

INSERTC, INSERTL, INSERTN, and INSERTO


Chapter Contents

Previous

Next

Top of Page

Copyright 1999 by SAS Institute Inc., Cary, NC, USA. All rights reserved.