Any way to listitems through tables?

Discuss any general programming issues here
Post Reply
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Any way to listitems through tables?

Post by Bugala »

I am looking for some good solution, basically it dont need to be this one.

Normally I would be having "IF Listitems(table) > 0 THEN something".

However, in this particular case it contains info in way of "name=andreas"

Hence, is there any way to check if this table is empty or not in a simple manner that i can use in one line to use for IF - THEN condition keeping the code still clean? I dont think NIL will work in this case, as that table does exist always, just that the table might be empty.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Any way to listitems through tables?

Post by Allanon »

Hello Bugala,
In the table library I've release some time ago there is a function

TB.Count()

it counts also non-numeric indexes so you can use that code for your purposes

-> http://forums.hollywood-mal.com/viewtop ... tial#p4494
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to listitems through tables?

Post by Bugala »

ah, forgot I actually saw that there.

I once, not long ago downloaded those from Indiego in hopes of them solving one problem I had back then. Have to take a look at that if that would be the solution to my problem.

I just remember that last time i was trying to use something (which wasnt even exact solution to my problem, what ever it was...) the main problem that i disliked was that I couldnt just include one of the hws files, but they had dependencies upon other hws files in the end making it seem like it is easiest to simply put all those hws files there, and for some reason i disliked doing that.

What I was really hoping had been possibility to simply copy-paste that function to my program without need for any of the rest.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

Re: Any way to listitems through tables?

Post by Allanon »

Sure, you can copy & pasto only what ou need :)
I've structured my libs with includes and dependancies because I hate to type always the same things but also because using includes it's simple to maintain code, in case of a bug just a fix and a recompile is needed. Otherwise you have to hunt where all that piece of code have been used and fix it each time ;)
User avatar
TheMartian
Posts: 109
Joined: Sun Feb 28, 2010 12:51 pm

Re: Any way to listitems through tables?

Post by TheMartian »

Hi

I asked myself the same question a few days ago and came up with the following solution...

The code iterates through the list using the Pairs() iterator and simply counts the number of iterations. So in this case the answer you get is 3. Be aware that any item in the table set to NIL does not count (and should not because it is not there :-))

mytable={item1=42,item2="Fred",item3={"A","B","C"}}

Function CountItemsInList(tbl)
Local i=0
/* DisableLineHook */
For index,key In Pairs(tbl) Do i=i+1
/*EnableLineHook */
Return(i)
EndFunction

NPrint(CountItemsInList(mytable))

WaitLeftMouse
End


Interestingly you can try to take advantage of the new option in version 6.0 to disable the 'housekeeping proces' during the iterations. (Using DisableLineHook() and EnableLineHook()). I would not expect key activity or videoupdates to be required while executing this particular type of code. So the trick may be applicable in these conditions, and it speeds up the code substantially even with small tables of, say 12 items. On my Windows machine there was about a 25% improvement. I have not bothered to measure the effect with a list with hundreds of items though. And on the Amiga it may behave differently for better or worse. Of course with a few items the benefit is not noticable. But if you have, say a playfield with 200 * 200 fields, and each field has a couple of tables as subitems and you need the number if items of each table, then it starts to matter.

regards
Jesper
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Any way to listitems through tables?

Post by Bugala »

Thanks from your answer TheMartian, that speed part was quite interesting and could be useful in future.

In this case i was looking for some one line solution, like listitems had been. Of course it still is a one line solution if i make a function. I was about to make a function using same Pairs() iterator, except i was planning to use in way of:

function mypair(table)
found=false
for index, keu in Pairs(table) do found=true
return(found)
endfunction

since that had been enough for me, but for future needs good to know those enable hooks things in case i am in need of speed, plus it is actually better that it returns a number of items instead of just false/true.

In the case i was asking this, I actually noticed that other reason made me change the table to be different kind, and that change actually made the "listitems" to work again.
Post Reply