GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Download
  • News
  • Community
  • Resources
  • Language Reference
  • SmallBASIC Manual

This view

  • Original page
  • New address

GeezerVison is showing smallbasic.github.io — SmallBASIC | IN

Edit History

IN

a IN b

Home > Reference > Language

  • 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

a = [4,8,2,3,9]
for i in a
    print i
next
'Output 4 8 2 3 9
a = ["duck", "dog", "cat"]
for i in a
    print i
next
' Output duck dog cat

Example 4: Count files

myFiles = FILES("*")

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

title "Use IN to check if sub-string exists within string (see also INSTR):"
s = "Hello 5"
? rev("   s =" + q(s))
? "  "; 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

title "Use IN to return the index of matching element in array (1-Based):"
Dim a()
a << "Hello" ' append (<<) three elements to a array
a << "World"
a << 123
'a << [4,"x",6] ' nested array conflicts with the other types.
? rev("   a = " + Str(a) + " ")
? "  "; 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)

title "Use IN to flatten multi-dimension array:"
a = [1, 2, 3; 4, 5, 6; 7, 8, 9, 10, 11; "Hello", "World"]
? rev("   a = " + Str(a) + " ")
? "   ";
For i In a
  ? i; ", "; ' i is used as pointer to a array element inside FOR loop
Next
?
title "Use IN to print nested arrays:"
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9, 10, 11], ["Hello", "World"]]
? rev("   a = " + Str(a) + " ")
? "   ";
For i In a
  ? i; ", "; ' i is used as pointer to a(i) inside FOR loop
Next
?
title "Use IN to print map array:"
a = Array("{x:100, y:300, point:Top-Left}")
? rev("   a = " + Str(a) + " ")
? "   ";
For i In a
  ? i; ":"; a(i); ", "; ' i is used as pointer inside FOR loop
Next
?
title "Use IN to test equality of numbers:"
n = 10.5
? rev("   n = 10.5 ")
? "   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   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   anomail.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  
This page was last edited on Thu, 13 Apr 2023 16:31:29 +0200 | Markdown processed with pandoc 3.1.12.1
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.