Arrays and the Stuff Contained in the Squiggly Lines
Posted: Wed Jan 09, 2019 3:42 pm
First, just a general note, the squiggly lines while the general term for parenthesis, the squared things and the squiggly things I have been informed is braces for all three combined (at least that is what it is in the lexer I use and modified for escript as well as a C# book I am reading through), I generally refer each individual symbol as follows:
Parenthesis - Parenthesis
squared things - Brackets
squiggly things - Braces
I don't know what is correct but just throwing this out there so that maybe terminology can be standardized, I have seen "experts" who teach classes refer to Braces (squiggly things) as "squiggly things" or "squiggly parenthesis".
The above was just filler because I actually have a real tip buried in here for those not in the know which until last night, I was one of them til I did a minor bit of testing.
On to the Tip!
So for millennia I have been wanting an easy way to get the position of an element within an array without having to 'for' through the array (okay not millennia but months). Last night while writing a method for a religion system, a light bulb turned on in my head and I thought "let me try a simple check to see if x was in array" and see what that returns, if it returned simply "true" (1 in our case), then I would bug Turley for a feature request so let's take a look at a simple dot command I wrote and I will tell you what the outcome was.
my array contains 6 numbers, when I ran the dot command I was expecting a simple boolean type return (1 or 0 in our case) and was not expecting anything more or less from it.
So I declare my array and in another variable 'in_array' I have that equal 'CInt( text ) in my_array', then I use SendSysMessage to output the result.
The result was, either false (0 for escript) if the number was not found within the array OR if the number was in the array, I am so excited, it is the small things really, the result was the position within the array the value was found!
So to make a long-winded, drawn out post a little bit longer but with a summary so worth skipping to this point:
IF you need to find the position something may be within an array, using something like:
You can get the position within the array that the name Fred may be, you can then use this variable in a similar fashion to just using an if statement to check to see if something is in an array.
Parenthesis - Parenthesis
squared things - Brackets
squiggly things - Braces
I don't know what is correct but just throwing this out there so that maybe terminology can be standardized, I have seen "experts" who teach classes refer to Braces (squiggly things) as "squiggly things" or "squiggly parenthesis".
The above was just filler because I actually have a real tip buried in here for those not in the know which until last night, I was one of them til I did a minor bit of testing.
On to the Tip!
So for millennia I have been wanting an easy way to get the position of an element within an array without having to 'for' through the array (okay not millennia but months). Last night while writing a method for a religion system, a light bulb turned on in my head and I thought "let me try a simple check to see if x was in array" and see what that returns, if it returned simply "true" (1 in our case), then I would bug Turley for a feature request so let's take a look at a simple dot command I wrote and I will tell you what the outcome was.
Code: Select all
use uo;
use os;
program test_array( mobile, text )
var my_array := Array{ 1, 2, 3, 4, 5, 20 };
var in_array := CInt( text ) in my_array;
SendSysMessage( mobile, ""+in_array, 0x3, 0x42 );
return 1;
endprogram
So I declare my array and in another variable 'in_array' I have that equal 'CInt( text ) in my_array', then I use SendSysMessage to output the result.
The result was, either false (0 for escript) if the number was not found within the array OR if the number was in the array, I am so excited, it is the small things really, the result was the position within the array the value was found!
So to make a long-winded, drawn out post a little bit longer but with a summary so worth skipping to this point:
IF you need to find the position something may be within an array, using something like:
Code: Select all
var in_array := "Fred" in array_of_names;