How to convert "if($var&1)" to Xojo?

Hello can anybody help me with this?

I have a php statement that I want convert to XOJO

// $score is a String typecasted to Int
// $before is an array of strings
 
if (isset($before[$i])) {
                $score	= (int)$before[$i];
                if ($score & 1) {	// only odd
                    //$part .= $score; // debugging
                    $parts[] = $part;	
                    $part = '';
                }
            }

It looks like Bitwise And, but Xojo returns UInt64 and not boolean. So I guess I can’t use it like here.
Does anybody have any ideas?
Thank you

usually you do bitwise AND , then test the result for <> 0

eg

 if   ( 3   AND  2)   <> 0  then
//do stuff

Also, in this case, you can use mod:

if ( score mod 2 ) = 1 then // it's odd
// Mod uses division behind the scenes, so it's slower
If (score And 1) > 0 Then ... // Odd

Thank you all. This seems to work.
I’m still trying to find a solution for “isset($array[key])”
The only thing I can think of is a method IsSet(Array, Key)
That tries to read Array(Key) that catches Outoffbound exceptions and returns true or false and resets #Pragma to default
Or does anyone have a better idea.
Thank you

IsSet() tests for null, doesnt it?

isset

(PHP 4, PHP 5, PHP 7, PHP 8)

isset — Determine if a variable is declared and is different than null

so

if before(index) <> null then

should do it.

For range checking, maybe…

if (index >= 0) and (index <= before.lastIndex) and before(index) <> null then

the first two tests will eliminate out of range errors before the third is evaluated

1 Like