array.size() and Len(array)
-
innominabile
- Adept Poster
- Posts: 85
- Joined: Wed Aug 30, 2006 5:24 pm
array.size() and Len(array)
There is a difference or .size() and Len() are the same thing?
-
innominabile
- Adept Poster
- Posts: 85
- Joined: Wed Aug 30, 2006 5:24 pm
Tested on 097, no code related to the method .Size() or Len() has been changed between core versions.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.
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
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" }
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...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.
Shinigami
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.
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.
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
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':
Or, make sure your array really is an array (more explicit, which is always better, IMO!)
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:
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:
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)Code: Select all
if (typeof(retval) == typeof(array))
...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
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
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