PtokaX forum

Development Section => Your Developing Problems => Topic started by: RobinHood on 04 February, 2004, 10:32:05

Title: how to show the whole Array?
Post by: RobinHood on 04 February, 2004, 10:32:05
is it possible to show the whole array, perhaps sorted?

for example i've something like this:

userArray[user.sName]=arg

can i show all items sorted by username ?
can i show all items sorted by arg?
if it's not possible, can i show all unsorted?
can i search f?r args?

perhaps someone can help?

thx
Title:
Post by: c h i l l a on 04 February, 2004, 12:09:03
you can only sort associative tables.

so first you need to create a associatve table from you non associative one

then sort it for what you need.
then read it out.

if your table is very big then you should also use a search tool for searching non associative tables

check for thread "table search"

so it is possible :)
Title:
Post by: RobinHood on 04 February, 2004, 14:54:18
thx for your reply, but your answer won't help me :-(

i've scripted a hub script for my hub, but i still haven't understood tables :-(

i don't know their function and i d don't know how to create a table :-(

i can create an array and i can show arg if i've the username, but i cannot show all items.

but perhaps sometimes i will understand this., too ;-)

i think my script has to wait until this moment :-(
Title:
Post by: NotRabidWombat on 05 February, 2004, 04:22:11
Hi,

I'm guessing you've programming in some C like language that implements arrays by a sequence of the variables that are the same type.

In lua, arrays have been replaced with tables. Tables can be used like arrays with 1 based number indexes. They can also be used like associative arrays (or a hash table). These associative arrays take in string indexes to find your desired element. The element the index represents can be anything and can be different for each element in the table.

So you can see, there is a real difficult time sorted an associative array, which you are using. The best way to do it is to have two tables. The first is like the one you have. You use a user name or string argument to index. You have it point to a table that contains the data you want (tables are the only things you can really point to and have multiple pointers to the same table since they are always passed by reference).
The table you are pointing to will contain the argument you used to index and the value you want to store. Now you can copy these table elements into a second table that uses number indexes. Use the sort function to sort it and now you have exactly what you want.

If you'd like a working example, I do the same thing in my JumbleFever script.

If you'd just want to view the entire contents of a table, you can use the foreach function. Go here: http://www.lua.org/manual/4.0/manual.html for more information.

-NotRabidWombat
Title:
Post by: RobinHood on 05 February, 2004, 10:15:35
thx NotRabidWombat,

i found another thread :-)

now i can show all items

an i wrote a code to search for args

your description about sorting sounds difficult *g*

so i think i will make it unsorted, because i can search for args :-))