Finding non-empty elements in an array.

Post » Thu Nov 14, 2013 5:21 am

The "Length" member returns the declared size of an array, not the number of elements inside. I need to sift through various arrays and compare their element count to avoid doing unnecessary work. For now, I made a small function:

int Function getElementCount (arrayType[] array)    int counter = 0    while (array[counter])        counter += 1    endwhile    return counterEndFunction

But it has two disadvantages: It only works for a single data type and it creates extra overhead depending on the array size and quantity.

Is there a built-in way of obtaining the pupulated elements in an array?

Thank you.

User avatar
Nana Samboy
 
Posts: 3424
Joined: Thu Sep 14, 2006 4:29 pm

Post » Thu Nov 14, 2013 1:23 pm

There's no built-in function to do this.
Also, your definition of empty already depends on the data type. Just because an integral type has its default value, that doesn't mean it's empty (0 is a perfectly fine number :smile:).

Then on the other hand, I don't really know what you want to do anyway. The example code you pasted doesn't really count the number of empty elements, it returns the index+1 of the first element that is false when implicitly converted a boolean. For this, there is a built-in function (find), but I'm not sure if that's what you want.
User avatar
Alyna
 
Posts: 3412
Joined: Wed Aug 30, 2006 4:54 am

Post » Thu Nov 14, 2013 1:47 am

If your array is organized so that the first empty element marks the end of the array, you can just use

yourArray.Find(false)

to return the total number of filled elements (as Schlangster suggests). Essentially this line of code does exactly what your function above does, only much faster.

User avatar
Felix Walde
 
Posts: 3333
Joined: Sat Jun 02, 2007 4:50 pm

Post » Thu Nov 14, 2013 4:25 am

Well, technically the definition of empty depends on your point of view. While in some areas of mathematics zero is just another number, in Set Theory zero is defined as the empty set, ?, a set with no elements. You are right however, zero can be a valid datum for an array of integers. Luckily I'm not dealing with integers or need to check for the digit zero. If I did, I would have to explicitly check for it, as in: if (X == 0) and using boolean logic would be a terrible mistake.

My apologies for not being clearer. I simply wish to obtain the number of elements that, considering boolean logic, are true in my array.

That wasn't my intention. I wanted the opposite: to count the number of filled elements.

A shame But thank you.

User avatar
stevie critchley
 
Posts: 3404
Joined: Sat Oct 28, 2006 4:36 pm

Post » Thu Nov 14, 2013 10:57 am

Thank you very much!. That's exactly what I needed. I just tested it and it works perfectly. array.Find(none)
User avatar
Hilm Music
 
Posts: 3357
Joined: Wed Jun 06, 2007 9:36 pm

Post » Thu Nov 14, 2013 12:40 pm

Chesko posted a set of very nice http://www.gamesas.com/topic/1365378-exampleresourcegeneral-purpose-array-functions/. His ArrayHasForm() is no longer needed as since 1.6 Find() is included. Because his functions are defined for the type Form any http://www.creationkit.com/Category:Script_Objects can be used, so not ints, floats, bools, strings or aliases.

User avatar
carrie roche
 
Posts: 3527
Joined: Mon Jul 17, 2006 7:18 pm


Return to V - Skyrim