Arrays and the Stuff Contained in the Squiggly Lines

Sometimes you need to share your knowledge. This is where you can do it. A place for POL and Script guides.
Post Reply
ThisIsMe
Distro Developer
Posts: 74
Joined: Sun Jul 17, 2016 1:29 am
Contact:

Arrays and the Stuff Contained in the Squiggly Lines

Post by ThisIsMe » 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.

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
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:

Code: Select all

var in_array := "Fred" in array_of_names;
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.

DevGIB
Grandmaster Poster
Posts: 210
Joined: Mon Feb 06, 2006 6:12 am

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by DevGIB » Wed Jan 09, 2019 5:54 pm

That's a pretty good find!

Yukiko
Distro Developer
Posts: 2586
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by Yukiko » Wed Jan 09, 2019 11:11 pm

I admit when ThisIsMe showed me that, well in reality, told me about it I was impressed, surprised, happy, and disappointed; Impressed because he found a tid bit that I have never seen anyone use, surprised because whomever wrote the in function in the Core had the forthought to provide that functionality, happy because this will save us the hassle of foreaching through arrays to find out if/where an element exists in the array, and disappointed that I was not the one who discovered this :D

Good find son. :cheers:
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

DevGIB
Grandmaster Poster
Posts: 210
Joined: Mon Feb 06, 2006 6:12 am

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by DevGIB » Thu Jan 10, 2019 1:10 am

Not disappointed it wasn't in the documents ?? :P
If we had wiki docs it could be added RIGHT NOW! :P

Yukiko
Distro Developer
Posts: 2586
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by Yukiko » Thu Jan 10, 2019 11:28 pm

It could be added right now if the docs were updated and pushed to the repo.

If someone wants to create a wikified docs that can be downloaded for offline use as the current docs can then I have zero objections. The issue is that most of the time wikis are only usable online.

As for being disappointed it was not in the docs, I guess I have grown accustomed to missing documentation. :P
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

Yukiko
Distro Developer
Posts: 2586
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by Yukiko » Fri Jan 11, 2019 12:13 am

escript.inc updated. Pull request submitted. If I did everything properly as far as the pull request and syntax for the HTML format of the file is concerned then as soon as the pull request is approved it will be in the docs.
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

Turley
POL Developer
Posts: 651
Joined: Sun Feb 05, 2006 4:45 am

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by Turley » Sat Jan 12, 2019 12:57 am

Ah that's the story behind the pull request ;)
It didn't exist since the beginning but since several years, atleast it mentioned in the core-changes, does any other doc even count? :P
02-15-2011 XXX:
Changed: "in" keyword now returns the arrayindex or 0 if no match was found

Yukiko
Distro Developer
Posts: 2586
Joined: Thu Feb 02, 2006 1:41 pm
Location: San Antonio, Texas
Contact:

Re: Arrays and the Stuff Contained in the Squiggly Lines

Post by Yukiko » Sat Jan 12, 2019 2:21 am

Ha ha ha!
Uhm... I read through core-changes.txt when I was adding settings to the Configurator. It counts but if that was all the documentation we had it might drive me sane. I would say insane but it is not possible to drive me to the place I am already. :D

I will have to post the "documentation" we had before Racalac created the docs page we are familiar with today. Core-changes.txt is better than what passed for docs in "the olden days".
Sincerely,
Yukiko

I would tell you a UDP joke but you might not get it.

Titus 2:13

Post Reply