Srch: Difference between revisions

From BR Wiki
Jump to navigation Jump to search
m (1 revision)
 
m (Added the fact that -1 is returned if not found. And that ^ makes it search for sub-strings)
 
(12 intermediate revisions by 3 users not shown)
Line 1: Line 1:
Srch(array-name,argument[,row])


Srch(array-name$,argument$[,row])
The '''Srch''' [[internal function]] searches an [[arrays|array]] and returns the row number matching the argument. If the argument is not found, then either 0 (BR 4.1 and below) or -1 (BR 4.2 and above). The argument must be the same data type (string or numeric) as the array. The optional "row" parameter defines the starting array element for the search.


The '''Srch''' [[internal function]] searches an [[array]] and returns the row number matching the argument. The argument must be the same data type (string or numeric) as the array. The optional "row" parameter defines the starting array element for the search.
SRCH(<array-name>,<argument>[,<row>])
 
OR
 
SRCH(<array-name$>,[^]<argument$>[,<row>])


====Optional Case Insensitivity and Substring matching====
====Optional Case Insensitivity and Substring matching====
If argument$ begins with ^ then the ^ is stripped and the search becomes '''case insensitive''' AND str2$ is '''regarded as a substring'''.
 
If argument$ begins with the caret ^, then the ^ is stripped and the search for argument$ in array-name$ becomes '''case insensitive''' (as of 4.2). Also, when the caret ^ is specified, the search is performed for sub-strings instead of whole strings.


For example:
For example:


  a$(1)='abc'
00010 let a$(1)='abc'
  a$(2)='def'
00020 let a$(2)='def'
  srch(mat a$,'^B')   returns    1
00030 print srch(mat a$,'^B')
 
Output:
 
1
 
Given the (4.2+) SRCH use of the "^" character, to search for '^' with
"let X=POS(string$,'^',5)" you will need to either turn the search character
off with CONFIG SEARCH_CHAR OFF or replace the search character with something else as in CONFIG SEARCH_CHAR 7E (where 7E is the hexadecimal representation of an alternate search character).


====Comments and Examples====
====Comments and Examples====
As of 4.2, SRCH returns zero if it fails (instead of -1) unless OPTION BASE ZERO
is in effect or OPTION 56 is in effect.
For versions 4.1 and earlier, when the search is unsuccessful, Srch returns -1. Using OPTION [[BASE]] 0 will also cause SRCH to return a zero.
In line 510 below, if STATES$ is a string array containing the 50 valid two-letter state abbreviations, then a data entry program could check whether the operator entered a correct abbreviation into the variable ST$ by the following:
500 LINPUT ST$
510 If Srch(Mat States$,ST$) = 0 then goto 500
The example below will find the selected item in a [[combo box]]
The example below will find the selected item in a [[combo box]]


500 combo_choice$=srch(mat array-name$,"^^")
500 combo_choice$=srch(mat array-name$,"^^")


When the search is unsuccessful, Srch returns -1 (unless [[option (config)|option]] 56 is enabled, in which case srch would return a 0). In line 510 below, if STATES$ is a string array containing the 50 valid two-letter state abbreviations, then a data entry program could check whether the operator entered a correct abbreviation into the variable ST$ by the following:
====IMPORTANT====


500 LINPUT ST$
Earlier versions of BR return 0 when SRCH doesn't find the desired argument in the array. Later versions return -1 in the same situation. In order to make you programs produce the same results regardless of the BR version, use the following logic:
  510 If Srch(Mat States$,ST$) = -1 then goto 500
 
  00010 if not SRCH(mat array$, string_to_find$) > 0 then
00020    ! add the code for when the result is NOT found
00030 else
00040    ! add the code for when the result IS found
00050 end if


====Related Functions====
====Related Functions====
*[[AIdx]]
*[[AIdx]]
*[[DIdx]]
*[[DIdx]]

Latest revision as of 00:10, 28 December 2015

The Srch internal function searches an array and returns the row number matching the argument. If the argument is not found, then either 0 (BR 4.1 and below) or -1 (BR 4.2 and above). The argument must be the same data type (string or numeric) as the array. The optional "row" parameter defines the starting array element for the search.

SRCH(<array-name>,<argument>[,<row>])

OR

SRCH(<array-name$>,[^]<argument$>[,<row>])

Optional Case Insensitivity and Substring matching

If argument$ begins with the caret ^, then the ^ is stripped and the search for argument$ in array-name$ becomes case insensitive (as of 4.2). Also, when the caret ^ is specified, the search is performed for sub-strings instead of whole strings.

For example:

00010 let a$(1)='abc'
00020 let a$(2)='def'
00030 print srch(mat a$,'^B')

Output:

1

Given the (4.2+) SRCH use of the "^" character, to search for '^' with "let X=POS(string$,'^',5)" you will need to either turn the search character off with CONFIG SEARCH_CHAR OFF or replace the search character with something else as in CONFIG SEARCH_CHAR 7E (where 7E is the hexadecimal representation of an alternate search character).

Comments and Examples

As of 4.2, SRCH returns zero if it fails (instead of -1) unless OPTION BASE ZERO is in effect or OPTION 56 is in effect.

For versions 4.1 and earlier, when the search is unsuccessful, Srch returns -1. Using OPTION BASE 0 will also cause SRCH to return a zero.

In line 510 below, if STATES$ is a string array containing the 50 valid two-letter state abbreviations, then a data entry program could check whether the operator entered a correct abbreviation into the variable ST$ by the following:

500 LINPUT ST$
510 If Srch(Mat States$,ST$) = 0 then goto 500

The example below will find the selected item in a combo box

500 combo_choice$=srch(mat array-name$,"^^")

IMPORTANT

Earlier versions of BR return 0 when SRCH doesn't find the desired argument in the array. Later versions return -1 in the same situation. In order to make you programs produce the same results regardless of the BR version, use the following logic:

00010 if not SRCH(mat array$, string_to_find$) > 0 then
00020    ! add the code for when the result is NOT found
00030 else
00040    ! add the code for when the result IS found
00050 end if

Related Functions

Technical Considerations

  1. If a match was not found, Srch will return -1.