Get number of houses from array?
#1

Hi,

I was wondering, is it possible to count how many houses their are without using mysql.

I've been doing it by getting the row count from the database, but I feel like their must be an easier way to do it.

Thanks.
Reply
#2

why don't you just use row count? it is harder to count an array. you'll have to loop through all elements until it reaches an unused cell.
Reply
#3

If that's what you think is best, I'll continue doing so.
Reply
#4

Quote:
Originally Posted by PawnHunter
Посмотреть сообщение
why don't you just use row count? it is harder to count an array. you'll have to loop through all elements until it reaches an unused cell.
And mysql has to send request, wait for mysql server to select all rows, count them and than return result. Looping thru array is 1000x times faster (literally).


Just loop thru your houses and skip unused cells/rows. My simple trick is to check if houses entrance Z is 0 or if label/icon is invalid. Simple example:

pawn Код:
stock CountHouses()
{
    new count = 0;
    for(new i = 0; i < MAX_HOUSES; i++)
    {
        if(HouseInfo[i][EntZ] != 0.0) count++;
    }
    return count;
}
While EntZ should be float and comparing floats is discouraged feel free to do it in pawn.
Reply
#5

Quote:
Originally Posted by DRIFT_HUNTER
Посмотреть сообщение
Just loop thru your houses and skip unused cells/rows.
Storing the number of houses loaded from the database when the server starts in a global variable and increasing/decreasing by 1 when a house is created/deleted respectively is the best way.
Reply
#6

Quote:
Originally Posted by Konstantinos
Посмотреть сообщение
Storing the number of houses loaded from the database when the server starts in a global variable and increasing/decreasing by 1 when a house is created/deleted respectively is the best way.
Or that
Reply
#7

If the only purpose of a query is to get a count of something then the COUNT() aggregate function should be used. Sending over a single value is obviously much faster than sending over an entire result set which isn't going to be used.
Reply
#8

Or more simple, use foreach & y_iterator.
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)