Skip to content

Instantly share code, notes, and snippets.

@cydh
Created October 18, 2018 06:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cydh/1b7deee82bc9f550acb4e8755472ba9b to your computer and use it in GitHub Desktop.
Save cydh/1b7deee82bc9f550acb4e8755472ba9b to your computer and use it in GitHub Desktop.

Sum elements of numeric array(s) for rAthena

//////////////////////////////////////////////////////////////////////////////////
// Sum elements of numeric array(s)
// Example:
//   setarray .@array1[0],2,3,1,5;
//   setarray .@array2[0],-3,-5,1;
//   .@total = callfunc("F_ArraySum",.@array1);          // will returns 11
//   .@total = callfunc("F_ArraySum",.@array1,.@array2); // will returns 4
//////////////////////////////////////////////////////////////////////////////////
function	script	F_ArraySum	{
	.@sum = 0;
	for (.@i = 0; .@i < getargcount(); .@i++) {
		// TODO: Create command to check var is numeric, then sum it directly
		//if (is_number(getarg(.@i))) {
		//	.@sum += getarg(.@i);
		//	continue;
		//}
		for (.@j = 0; .@j < getarraysize(getarg(.@i)); .@j++)
			.@sum += getelementofarray(getarg(.@i),.@j);
	}
	return .@sum;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment