generic image
Processing...
  • Games
  • Catalog
  • Develop
  • Robux
  • Search in Players
  • Search in Games
  • Search in Catalog
  • Search in Groups
  • Search in Library
  • Log In
  • Sign Up
  • Games
  • Catalog
  • Develop
  • Robux
   
ROBLOX Forum » Game Creation and Development » Scripters
Home Search
 

Oddities in the way which Lua handles arrays

Previous Thread :: Next Thread 
NXTBoy is not online. NXTBoy
Joined: 25 Aug 2008
Total Posts: 4533
13 Feb 2013 06:19 PM
Here's another way of looking at array indexing - count gaps between entries. Considering the array `{'A', 'B', 'C', 'D', 'E'}`, we can number the "gaps" or "edges" between entries:

    |A|B|C|D|E|
    0 1 2 3 4 5

In lua, we name the entry by its upper edge. In C, we name the entry by its lower edge.

So why not start arrays at `0.5`, since that falls right in the middle of the entry?
Report Abuse
BJJScrambler is not online. BJJScrambler
Joined: 13 Jan 2013
Total Posts: 898
13 Feb 2013 07:22 PM
> Are there any advantages to using tables over arrays in a language, or vice versa? With arrays, they limit you to a static data structure, and only allows one type of data type. If Lua uses tables, why do arrays exist in other languages?

The only real advantage to arrays over tables is that they are less complex structures and thus take up less memory in RAM.

Tables allow you to store any object under any index. This means that you can use them to in a way store conditionals:

local tab={
[true]=function() print("yes") end,
[false]=function() print("no") end
}

tab[ 2<3 ] -->yes

or you can use them to store information of players or past objects without having to use rigorous search algorithms to find the information in a table by just storing them by their name, or something else of the sort.

Also the availability of negative numbers, doubles and vectors as indexes make mapping a place a lot easier.
Report Abuse
HotThoth is not online. HotThoth
Forum Moderator
Joined: 24 Aug 2010
Total Posts: 1176
14 Feb 2013 02:03 PM
To put things into "standard computer science jargon", Lua tables are really just "hashtables", with a little extra functionality to allow them to better act as "vectors".

All of these different data structures have their own advantages and disadvantages. To use arrays and tables as an example, here are a few simple differences:

tables:
- can be dynamically resized
- can be indexed with an arbitrary key-type to store an arbitrary value-type
- can be easily used to mimic the functionality of other data structures, like sets, vectors/stacks, queues, linked lists, trees
- elements in a hashtable are not necessarily "nearby" in memory
- requires a good hashing function in order to have good performance

arrays:
- require a fixed amount of space which must be known beforehand
- indexing into an array or changing an element is much faster
- an array of N things takes much less memory than a table of N things
- you're guaranteed to have everything in an array "nearby" in memory
- needs no key/value pairings and does not need a hashing function

In order to restrict the language specification to being as simple as possible, Lua uses only tables because it is possible to mimic most other data structures using tables, although these will not be as performant as data structures in other languages (but for Lua, we generally want more high-level coding, so this should not be as big of a concern to us). So tldr: easier, less performant.

For a language like C/C++, it makes sense that the "basic building block" would be "pointers" and "arrays" (an array is really just a pointer to what is assumed to be a contiguous block of memory of a certain length). All of the other data structures can then be built up from these (with a good deal of work), and can be controlled and optimized for the particular task at hand if necessary. So tldr: harder, more performant.

Since the "basic building block" of C/C++ is the pointer, it makes sense that indexing starts at 0, since we're always dealing with pointers and offsets from a particular pointer. Since Lua has no pointers (and no memory offsets to think about), it makes sense for Lua and other higher-level languages to index from 1, to follow a more conventional "human-intuitive" counting scheme.


- HotThoth

~ Think happy Thoths ~
Report Abuse
Previous Thread :: Next Thread 
Page 2 of 2Go to page: 1, [2] Prev
 
 
ROBLOX Forum » Game Creation and Development » Scripters
   
 
   
  • About Us
  • Jobs
  • Blog
  • Parents
  • Help
  • Terms
  • Privacy

©2017 Roblox Corporation. Roblox, the Roblox logo, Robux, Bloxy, and Powering Imagination are among our registered and unregistered trademarks in the U.S. and other countries.



Progress
Starting Roblox...
Connecting to Players...
R R

Roblox is now loading. Get ready to play!

R R

You're moments away from getting into the game!

Click here for help

Check Remember my choice and click Launch Application in the dialog box above to join games faster in the future!

Gameplay sponsored by:
Loading 0% - Starting game...
Get more with Builders Club! Join Builders Club
Choose Your Avatar
I have an account
generic image