Posted by
| Nick Gammon
Australia (23,051 posts) Bio
Forum Administrator |
Message
| I haven't extensively programmed in Python, Perl, or TCL (no time!), however having used VBscript a fair bit, and JScript less, I can comment to an extent. :)
The limitation I see with VBscript is its array handling - I think Shadowfyr has commented a while back that it is a pain to redimension arrays, and even just use them.
The nice thing about Lua, which I have been pushing recently, is its table management. Effectively its tables are like dictionaries and lists.
What that means is, that if you use numeric keys, generally starting at 1, then it is like a list, however one that can be expanded or contracted indefinitely.
Just to bang on about Lua for a bit more, I'll give an example. I'll create an empty table, insert 3 items, add a new one *to the middle*, and then delete the first one. After that, I'll sort it.
-- helper function to print the table
function printit (x)
print () -- blank line first
table.foreach (x, print) -- print keys and values
end
t = {}
table.insert (t, "Hello")
table.insert (t, "World")
table.insert (t, "Foo")
printit (t)
-- insert new item at position 2
table.insert (t, 2, "New")
printit (t)
-- remove item at position 1
table.remove (t, 1)
printit (t)
-- sort ascending
table.sort (t)
printit (t)
-- output:
1 Hello
2 World
3 Foo
1 Hello
2 New
3 World
4 Foo
1 New
2 World
3 Foo
1 Foo
2 New
3 World
This is pretty straightforward, yes? You can insert, delete and sort. Lua tables let you implement stacks, queues and double queues, simply by inserting or removing at each and as required.
However the other thing you can use them for is dictionaries or "maps". In this case you simply index directly to each entry:
t = {}
t ['name'] = "nick"
t ['hp'] = 22
printit (t)
print ("his name is", t.name)
-- output:
hp 22
name nick
his name is nick
Now the table is being used as a dictionary, where you can insert any item by key, and retrieve it the same way (as in the last line, printing t.name).
Now in fact Lua is designed to be very small and compact, with the real power being the ability to extend it with extra libraries. The PCRE library, which I incorporated into MUSHclient, is an example of that. Others are available on the web, such as a socket library, that - in about 4 lines of code once you install it - let you pull in an entire web page from another site using the HTTP protocol.
One other really nice thing is the ability to have optional arguments to function calls, and return multiple results.
|
- Nick Gammon
www.gammon.com.au, www.mushclient.com | Top |
|