## How to check if x is in list?

Pixilang programming language
philipbergwerf
Posts: 135
Joined: Sat Mar 17, 2018 4:23 pm

### How to check if x is in list?

How to check if x is in list?
I have created a list of black key numbers:

Code: Select all

``````// draw selected key
\$blck = new(36)
clean(\$blck)
\$blck[0]=2

\$blck[1]=5
\$blck[2]=7

\$blck[3]=10
\$blck[4]=12
\$blck[5]=14

\$blck[6]=17
\$blck[7]=19

\$blck[8]=22
\$blck[9]=24
\$blck[10]=26

\$blck[11]=29
\$blck[12]=31

\$blck[13]=34
\$blck[14]=36
\$blck[15]=38

\$blck[16]=41
\$blck[17]=43

\$blck[18]=46
\$blck[19]=48
\$blck[20]=50

\$blck[21]=53
\$blck[22]=55

\$blck[23]=58
\$blck[24]=60
\$blck[25]=62

\$blck[26]=65
\$blck[27]=67

\$blck[28]=69
\$blck[29]=71
\$blck[30]=73

\$blck[31]=76
\$blck[32]=78

\$blck[33]=81
\$blck[34]=84
\$blck[35]=86``````
I am coming from python where I could simply check this using:

Code: Select all

``````if x in \$blck:
# do something``````
How to form the similar if statement in pixilang?
Posts: 3568
Joined: Fri Jan 23, 2004 12:28 am
Location: Ekaterinburg. Russia
Contact:

### Re: How to check if x is in list?

Slow way:

Code: Select all

``````fn check( \$x, \$blck )
{
\$blck_size = get_size( \$blck )
\$i = 0 while \$i < \$blck_size
{
if \$blck[ \$i ] == \$x
{
//X IS IN THE LIST
ret( 1 )
}
\$i + 1
}
ret( 0 )
}

if check( \$x, \$blck )
{
//x found in \$blck
//do something...
}
``````

Optimized fast way:

Code: Select all

``````\$kcol = new( 86, 1, INT ) //Key color = \$kcol[ \$key ] //0 - white; 1 - black;
clean( \$kcol )
\$kcol[ 2 ] = 1
\$kcol[ 5 ] = 1
\$kcol[ 7 ] = 1
\$kcol[ 10 ] = 1
\$kcol[ 12 ] = 1
\$kcol[ 14 ] = 1
...
``````