array.size() and Len(array)

Get Help on scripting in POL with configurations, config setups, script trouble shooting, etc.
Post Reply
innominabile
Adept Poster
Posts: 85
Joined: Wed Aug 30, 2006 5:24 pm

array.size() and Len(array)

Post by innominabile »

There is a difference or .size() and Len() are the same thing?
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

Len() is for finding the length of strings.

array.size() says how many elements are in an array.
innominabile
Adept Poster
Posts: 85
Joined: Wed Aug 30, 2006 5:24 pm

Post by innominabile »

But Len(array) don't report the array length?
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

no idea actually. I use them seperately. One is for strings, one is for arrays.
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

Austin told me to use .Size() for arrays and Len() for strings. As I recall he said that Len() will work but .Size() is more effidient than Len() on arrays.
blah
New User
Posts: 12
Joined: Thu Feb 02, 2006 12:05 pm

Post by blah »

btw..
im not sure, but if array is empty array.size() will return 1.
but len(array) will return 0. (pol095)

Anyway we can still use 'if(array.size() < 1)' to check for empty arrays.
Yukiko
Distro Developer
Posts: 2826
Joined: Thu Feb 02, 2006 1:41 pm

Post by Yukiko »

Not sure about the Array.Size*( returning a 1 if empty but if that's so then:
if (Array.Size() <=1) might be what you want for an empty array test.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

well if you were checking if the array is empty or not wouldnt you just check if(array) anyway?
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post by Austin »

blah wrote:btw..
im not sure, but if array is empty array.size() will return 1.
but len(array) will return 0. (pol095)

Anyway we can still use 'if(array.size() < 1)' to check for empty arrays.
Tested on 097, no code related to the method .Size() or Len() has been changed between core versions.

arraytest.src (text command)

Code: Select all

use uo;
use os;

unload_scripts("arraytest");

program ArrayTest(mobile)
	var alpha := array; // Size == 0 ?
	var bravo := array{}; // Size == 0 ?
	var charlie := array{1, "A"}; // Size == 2 ?
	var delta := {}; // Size == 0? .. Wrong way to make an array but testing anyway.
	var echo := {1, "A"} // Size == 2? .. Wrong way to make an array but testing anyway.

	SizeTests("Alpha", alpha);
	SizeTests("Bravo", bravo);
	SizeTests("Charlie", charlie);
	SizeTests("Delta", delta);
	SizeTests("Echo", echo);

	SendSysMessage(mobile, "Done. Check the damned console for results.");

	return 1;
endprogram

function SizeTests(name, byref an_array)
	Print("--Testing "+name+"--");
	Print("Type is "+TypeOf(an_array));
	Print("Basic::Len("+name+") == "+Len(an_array)); // Should be used for strings only, its a feature that it works for arrays too.
	Print("array.Size() == "+an_array.Size());
	Print("\n");

	return 1;
endfunction
Console Results

Code: Select all

--Testing Alpha--
Type is Array
Basic::Len(Alpha) == 0
array.Size() == 0


--Testing Bravo--
Type is Array
Basic::Len(Bravo) == 0
array.Size() == 0


--Testing Charlie--
Type is Array
Basic::Len(Charlie) == 2
array.Size() == 2


--Testing Delta--
Type is Array
Basic::Len(Delta) == 0
array.Size() == 0


--Testing Echo--
Type is Uninit
Basic::Len(Echo) == 0
array.Size() == error{ errortext = "Method id '48' (size) not found" }
Shinigami
Former Developer
Posts: 308
Joined: Mon Jan 30, 2006 9:28 am

Post by Shinigami »

Austin wrote:

Code: Select all

	var delta := {}; // Size == 0? .. Wrong way to make an array but testing anyway.
	var echo := {1, "A"} // Size == 2? .. Wrong way to make an array but testing anyway.
who is saying that it is the wrong way? hmm? I'm using this in most cases because I'm lazy and it contains less character than the other way...

Shinigami
User avatar
Austin
Former Developer
Posts: 621
Joined: Wed Jan 25, 2006 2:30 am

Post by Austin »

Just plain {} existed before there were other data types. It defaults to an array. Mostly the brackets are now used to initialize the data in a structure, array or dictionary. Thats why I noted using plain {} isn't proper. As Rowan would put it.. its like sipping your tea, without your pinkie finger being out.
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

I'm wondering because I just ran into a situation like this if blah possibly had an error struct instead of an array. I just did a code like this...

var multis := ListMultisInBox(who.x-5, who.y-5, who.z-20, who.x+5, who.y+5, who.z+20, who.realm)
SendSysMessage(who, "There are " + multis.size() + " multis in this box.");


even when it was supposed to be 0, it was 1 but I'm figuring thats because of the "errortext" index of the error.
Marilla

Post by Marilla »

Yup. That is correct; If a function that's supposed to return an array instead returns an error struct, returnvalue.size() will return 1, because errors are a struct with one member. However, you must keep in mind that your returned array could, itself, have only a single member, so you can't just assume a returnvalue.size()==1 means you don't have a valid array.

There are a few ways you should get around this. First, test your value against error or 'false':

Code: Select all

if (!retvalue)
if (retvalue==error)
Or, make sure your array really is an array (more explicit, which is always better, IMO!)

Code: Select all

if (typeof(retval) == typeof(array))
...
You could also just check if typeof(retval)=="Array", since that's the value currently returned, but I favor checking it against the array keyword, in case the return value from typeof() ever changes.


And, as is common with me lately, a caveat!

An empty array will not evaluate as 'false'. Say you make a function:

Code: Select all

function GetDudes(who)
   var retval := array;
   foreach guy in EnumerateOnlineCharacters()
      if (guy != who)
         retval.append(guy);
      endif
   endforeach
   return retval;
endfunction

program ShowDudes(who)
   var dudes := GetDudes(who);
   if (!dudes)
      SendSysMEssage(who, "There are no dudes!");
   else
      ...show the dudes here...
   endif
endprogram
If this person is on alone, the return array from GetDudes will be an empty array. An empty array does not evaluate to 'false' (!dudes), though an error struct does. So we might change our code to:

Code: Select all

function GetDudes(who)
   var retval := array;
   foreach guy in EnumerateOnlineCharacters()
      if (guy != who)
         retval.append(guy);
      endif
   endforeach
   if (retval.size() == 0)
      return error{"errortext" := "You're the only dude!"};
   endif
   return retval;
endfunction
User avatar
CWO
POL Expert
Posts: 1160
Joined: Sat Feb 04, 2006 5:49 pm

Post by CWO »

well, of course with mine, I just did it like this...

Code: Select all

var multis := ListMultisInBox(who.x-5, who.y-5, who.z-20, who.x+5, who.y+5, who.z+20, who.realm)
if (multis)
     SendSysMessage(who, "There are " + multis.size() + " multis in this box.");
else
     SendSysMessage(who, "There are no multis in this box.");
endif
Post Reply