Pos: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
m (1 revision)
(No difference)

Revision as of 14:11, 15 January 2012

Pos(A$,B$[,X])

The Pos internal function returns the position of the first character of a substring in A$ that matches B$. Returns 0 if there is no match or B$ is null. X (optional) specifies the position on which to begin the comparison. When X is positive, the search moves toward the end of the string. When X is negative, the search moves backward toward the beginning of the string.

Caret Processing

As of 4.20 Pos supports enhanced ^ processing.

   * Pos(str1$,str2$ [, {-}start] ) 

If str2$ begins with ^ then the ^ is stripped and the search becomes case insensitive. A configuration statement will permit use of a flag character other than ^.

See also: SEARCH_CHAR

Comments and Examples

POS(A$,B$,X) returns the position in A$ of the first character of a substring in A$ that matches B$; the optional X parameter specifies the starting position in string A$. When X is positive, the search moves forward (toward the end of the string) starting at position X in A$.

When X is negative, the search moves backward (toward the beginning of the string) starting at position X from the end of A$. The program below will print the numbers 2, 2, 7 and 7.

00100 LET A$="WXYZ WXYZ"
00200 LET B$="XY"
00300 PRINT POS(A$,B$)
00400 PRINT POS(A$,B$,1)
00500 PRINT POS(A$,B$,POS(A$,B$)+1) ! find 2nd B$
00600 PRINT POS(A$,B$,-1)

The POS function is often used to determine if a string matches any items listed in a longer string. The crucial information is not the column position of the match, but the zero (when not found) or positive (when found) value returned. The example below illustrates reading a display file one character at a time by using EOL=NONE and dimensioning a variable to be used with LINPUT so that its maximum string length is one. Using POS in line 40, each incoming character is tested to determine which ones are vowels.

00010 DIM X$*1
00020 OPEN #1:"name=datafile,EOL=NONE",DISPLAY,INPUT
00030 LINPUT #1: X$ EOF DONE
00040 IF POS("AaEeIiOoUu",X$) > 0 THEN PRINT X$; " -is a vowel"
00050 GOTO 30
00060 DONE: CLOSE #1:

Related Functions