lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 9/12/06, Luiz Henrique de Figueiredo <> wrote:
> How can one easily find out the number of records stored in a table?
> I think that there should be a convenient idiom for such a common task.

Is it really a such a common task? I never felt the need for that.
Occasionally, some people need to know whether a table is empty or not,
and for that the idiom is "next(t)==nil".

Sure it is quite common. Just few examples:
1: Telephone book. Table contains pairs name = phone_number. I want to
know how many entries are in the phone book.
2: TV channel guide. Table contains pairs TV_station= channel_number.
I want to know how many channels did I sign up for.
3: Term exams grades. Table contains pairs student_name=test_score. I
want to know how many student took the test.
I can go on and on.
Besides many hash implementations do keep track of how many entries
are there in order to decide when to grow hash memory allocation. It
may be simply a matter of exporting such a counter to the API.

On a side note, Lua is a general purpose language used in many
different ways. Something that one programmer never uses could be very
handy for someone else.