array.size() and Len(array)
Posted: Tue Sep 19, 2006 9:18 am
There is a difference or .size() and Len() are the same thing?
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.
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.
Code: Select all
if (!retvalue)
if (retvalue==error)Code: Select all
if (typeof(retval) == typeof(array))
...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
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