{,}[]Arrays in Lua, JavaScript and PHP


In the AutoHotkey forum, I suggested this language would gain by having true array and associative array support. Since it is a late addition, syntax has to fit in the current language, with some adaptations since it will go through a v.2 supporting incompatible changes.

Anyway, I thought it would be a good idea to show how such objects are supported in some popular scripting languages. The demonstration is limited, by lack of time and knowledge: I don't know Python, I am not an expert of Perl (plus it has a quite exotic syntax), etc. So I chose three languages I know well enough, and tried to write similar scripts, or at least having similar output, while remaining native as much as possible.

It was very interesting and educational, I learnt a lot in the process... I hope it will be educational for you too.

This is not intended to be a reference, there are lot of good pages on the topic. So I just give code with some comment, you have to do the rest of the job...

The pages themselves are not very interesting: it is merely the output of the scripts, quite raw. But you can connect it to the corresponding lines of code, seing their result.

The bottom of these pages give instructions to access these sources: for JavaScript, just use the View Source feature of your browser; for PHP, I use the highlight_file function; and for Lua, I just link to the source code file (and to the HTML colorized version as output by SciTE).

Some insight

A quick survey of the differences between the languages.

Lua 5.0 allows to mix freely array (with numerical indexes) and table (associative array) in the same object. It maintains an array part, with quick numerical access to fields, as long as the indices are successive (without gap). Large disjoint indices and string keys are kept in a hash table.

Lua's philosophy for the base functions is to provide as little as possible while remaining convenient, ie. providing functions that cannot be done in Lua, or too slowly (like sort). The idea is that one can easily extend this list of functions with some Lua functions, or even C functions.

Note that I chose to stick to Lua 5.0 syntax and functions instead of using Lua 5.1's. But I made the output with Lua 5.1, because 5.0 has some quirks that I avoided...

JavaScript's Array object is strictly made for numerical indices. But it has the concept of string properties associated to an object which can have any values. Thus, a kind of associative array can be used. Many people use the Array object for that, but it has been shown to be a bad idea and I used the base Object for that.

JavaScript provides more array functions than Lua, so I coded the missing functions to keep on par and to show how it can be done. Note that some functions can be missing in some implementations (I tested in Firefox 2 and Internet Explorer 6), so one has to test the availability of these functions before running them. So depending on your browser, you might not see the same result as your neighbor... (I didn't provided workarounds.)

PHP 4+ can also store either with numerical or string indices.

PHP's array functions is significant of the quite anarchic development of the language... Everybody provided functions in the field, and no moderator tried to make them consistent, in name (shuffle vs. array_rand, sort vs. array_multisort) or in parameter lists. Having lot of functions might seem nice, but it is confusing to the user (which sort function to choose?) and lot of functions are probably rarely used and could easily be done with an iteration on the members (array_product...)