IN
a IN b
- b is an array: Returns the index (1 based) of the first matching element. If no matching element, IN returns FALSE.
- b is a string: Returns TRUE if the value is contained within b.
- for loop: FOR i IN array will cycle through all elements of array, i contains the element.
Example 1: array
print 1 in [-2,-1,1,2] ' Output 3
print "b" in ["a", "b", "c", "d"] ' Output 2
Example 2: string
print "a" in "car" ' Output 1
print "ar" in "car" ' Output 1
print "d" in "car" ' Output 0
Example 3: for loop
4,8,2,3,9]
a = [for i in a
print i
next
'Output 4 8 2 3 9
"duck", "dog", "cat"]
a = [for i in a
print i
next
' Output duck dog cat
Example 4: Count files
FILES("*")
myFiles =
for fl IN myFiles
if ".bas" IN fl then NumBasFiles++
if ".txt" IN fl then NumTxtFiles++
next
print "Number of .bas files: " + NumBasFiles
print "Number of .txt files: " + NumTxtFiles
Example 5
' IN keyword is 1-Based, so using "Option Base 1" makes code more consistent:
Option Base 1
Def q(text) = " " + Enclose(text) + " " ' Enclose text with quotation marks, ""
Def rev(text) = Cat(3) + text + Cat(-3) ' reverse colors of text
Sub title(text)
Color 14: Print text: Color 7 ' print title in color
End Sub
"Use IN to check if sub-string exists within string (see also INSTR):"
title "Hello 5"
s = " s =" + q(s))
? rev(" "; q("ll") + "In s? "; "ll" In s,, ' (0=FALSE; 1=TRUE)
? " "; q("LL") + "In s? "; "LL" In s
? " "; q("l") + " In s? "; "l" In s,,
? " 5 In s? "; 5 In s
?
"Use IN to return the index of matching element in array (1-Based):"
title Dim a()
"Hello" ' append (<<) three elements to a array
a << "World"
a << 123
a << 'a << [4,"x",6] ' nested array conflicts with the other types.
" a = " + Str(a) + " ")
? rev(" "; q("Hello") + "In a: "; "Hello" In a,,
? " "; q("ello") + " In a: "; "ello" In a
? " "; q("HELLO") + "In a: "; "HELLO" In a,,
? " "; q("World") + "In a: "; "World" In a
? " 123 In a: "; 123 In a,,
? " 12 In a: "; 12 In a
? '? "[4,\\"x\\",6] In a: "; [4,"x",6] In a ' (nested array)
"Use IN to flatten multi-dimension array:"
title 1, 2, 3; 4, 5, 6; 7, 8, 9, 10, 11; "Hello", "World"]
a = [" a = " + Str(a) + " ")
? rev(" ";
? For i In a
", "; ' i is used as pointer to a array element inside FOR loop
? i; Next
?"Use IN to print nested arrays:"
title 1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11], ["Hello", "World"]]
a = [[" a = " + Str(a) + " ")
? rev(" ";
? For i In a
", "; ' i is used as pointer to a(i) inside FOR loop
? i; Next
?"Use IN to print map array:"
title Array("{x:100, y:300, point:Top-Left}")
a = " a = " + Str(a) + " ")
? rev(" ";
? For i In a
":"; a(i); ", "; ' i is used as pointer inside FOR loop
? i; Next
?"Use IN to test equality of numbers:"
title 10.5
n = " n = 10.5 ")
? rev(" n In 10 : "; n In 10 ' (0=FALSE; 1=TRUE)
? " n In 10.5 : "; n In 10.5 ?
Code samples using IN
000 hello.bas
001 3 ways to print hello 5 times.bas
002 numeric variables.bas
003 conditional branching.bas
004 loops.bas
005 challenge.bas
006 arrays+.bas
100lines.bas
2048.bas
2048.bas
3d block lettes.bas
3d rotating cube with message.bas
3d wire cube v1.bas
3d wire cube.bas
3d_palmx.bas
3d_torus.bas
3dtictac.bas
3dtorus.bas
3dttt.bas
3dttt.bas
4tune.bas
7gables.bas
A Rubens, Peter Paul Landscape.bas
agendus.bas
ai.bas
amortig.bas
analog clock.bas
anball 1.0.bas
angel feather.bas
Language
AND
AS
BAND
BG
BOR
BYREF
CALL
CASE
CATCH
CONST
DECLARE
DEF
DO
ELIF
ELSE
ELSEIF
END
END TRY
ENDIF
EQV
EXIT
FALSE
FI
FOR
FUNC
GOSUB
GOTO
IF
IFF
IMP
IN
LABEL
LET
LIKE
LOCAL
LSHIFT
MDL
MOD
NAND
NEXT
NOR
NOT
ON
OR
REM
REPEAT
RETURN
RSHIFT
SELECT
STEP
STOP
SUB
THEN
THROW
TO
TRUE
TRY
UNTIL
USE
USG
USING
WEND
WHILE
XNOR
XOR
If there is insufficient information on this page and you wish learn more about IN,
please send an email to smallbasic@gmail.com. You can help to improve information about
IN by submitting a pull request,
click View Source for details. Note, an offline language reference text file is also available - see the Download section.