Jump to content
  • 0

Array start at 0


nok1a
 Share

Question

11 answers to this question

Recommended Posts

  • 0

in lua the array always start at index 1 , however you can force it to start with 0 like
 

table[0] = somthing


or 

table = {} 
for i = 0 , 10 do 
table[i] = somthing
end


or using metamethods
 

 

note : forcing it to start with index 0 isn't a good practice.

Edited by XEKEX
wrong solution
Link to comment
Share on other sites

  • 0

Note that metatable-based solution suggested above doesn't work as expected in all cases when key used in indexing access or indexing assignment exists in table, because "__index" and "__newindex" events are used only when key is not present in table. Though, XEKEX is right that it's not a good practice to have 0-based arrays in Lua. What is your use case for that and why 1-based arrays don't suffice?

Link to comment
Share on other sites

  • 0
18 minutes ago, CmP said:

Note that metatable-based solution suggested above doesn't work as expected in all cases when key used in indexing access or indexing assignment exists in table, because "__index" and "__newindex" events are used only when key is not present in table. Though, XEKEX is right that it's not a good practice to have 0-based arrays in Lua. What is your use case for that and why 1-based arrays don't suffice?

youre right forget that metatable doesn't work with numbers index

Link to comment
Share on other sites

  • 0
1 hour ago, XEKEX said:

in lua the array always start at index 1 , however you can force it to start with 0 like
 

table[0] = somthing


or 

table = {} 
for i = 0 , 10 do 
table[i] = somthing
end


or using metamethods
 

 

note : forcing it to start with index 0 isn't a good practice.

It doesn't start with 0 though. Which is rather the result i want to achieve.
  

image.png

Must be chronological order.

Edited by nok1a
added screenshot
Link to comment
Share on other sites

  • 0
1 hour ago, CmP said:

What is your use case for that and why 1-based arrays don't suffice?

It's for readability. In the game first offset contains no data(strings), i refer by it as sequence 0. Offset is 0 and size is 0 as well. Data only starts at the second offset. Which is actually sequence 1.

From there at index 0 i want the sequence 0 which contains no data, then at index 1 i want sequence 1 which contains data.

image.png

As you can see here,

image.thumb.png.2ed8e6cfd5d6e110f3cc31fb98c224e3.png

the empty string i not want at index 1. When at index 0 it will be more suitable for me when working with the game data. But i don't try to make it a habit. It's just this type of cases.

 

Edited by nok1a
added more info
Link to comment
Share on other sites

  • 0
16 minutes ago, nok1a said:

It's for readability.

For readability of the code or the output with table representation? If for the latter, one can simply create custom function to print table contents.

35 minutes ago, nok1a said:

It doesn't start with 0 though. Which is rather the result i want to achieve.
  

image.png

Must be chronological order.

There are no regular arrays in Lua, only tables that are associative arrays and can be used to implement many different data structures. Tables in Lua don't have starting element by themselves, but length operator and functions from "table" library operate only on part of table with consecutive integer keys starting from 1. Your interpretation is wrong, because tables don't have defined order of traversal, it's implementation-specific. In fact official Lua doesn't include table contents in it's default procedure to convert table to string. So instead of relying on default table to string conversion in GG implementation of Lua, implement custom function that will perform conversion the way you need.

Link to comment
Share on other sites

  • 0
On 4/3/2023 at 10:50 PM, CmP said:

For readability of the code or the output with table representation? If for the latter, one can simply create custom function to print table contents.

It is only for output table representation. Could you perhaps provide an example function for print table starting at index zero*? It's only for this particular case because in-game indexing starts at 0.

On 4/3/2023 at 10:50 PM, CmP said:

So instead of relying on default table to string conversion in GG implementation of Lua, implement custom function that will perform conversion the way you need.

Thanks lots for the explanation. I had no knowledge of anything you mentioned.

Edited by nok1a
Link to comment
Share on other sites

  • 0
17 minutes ago, nok1a said:

Could you perhaps provide an example function for print table starting at index zero*?

It's not more complicated than to print table elements starting from key 1. Looks more like the question is about how to print table elements at all. In the most basic case to print elements with integer keys starting from 0 and without printing contents of any nested tables it's enough to have one loop: 

local t = {[0] = 0, 1, 2, 3}

print("{")
local index = 0
local element = nil
while true do
  element = t[index]
  if element == nil then
    break
  end
  local representation = string.format("    [%d] = %s", index, element) -- implement custom type-dependent converion of value to string if needed, this one uses default conversion, i.e. string "1" and number 1 will result in the same output
  print(representation)
  index = index + 1
end
print("}")
Link to comment
Share on other sites

  • 0

Lua also allows to implement custom iterators to be used with generic "for", so one can implement traversal from 0 that way and use it like ipairs/pairs functions: 

-- Modified function from http://lua-users.org/wiki/IteratorsTutorial
function ipairs0(t)
  local function iterator(t, i)
    i = i + 1
    local v = t[i]
    if v ~= nil then
      return i, v
    else
      return nil
    end
  end
  return iterator, t, -1
end

-- Usage example
for i, v in ipairs0(t) do
  print(string.format("[%d] = %s", i, v))
end
Link to comment
Share on other sites

  • 0
On 4/3/2023 at 8:48 PM, nok1a said:

It doesn't start with 0 though. Which is rather the result i want to achieve.
  

image.png

Must be chronological order.

this is why it's not a good practice because ipairs , pairs and every built-in functions in lua that deals with tables start the iteration by default at index 1 
image.thumb.png.41d241fe770b93d8f92502f4ea02de40.png --> here is an example

Link to comment
Share on other sites

  • 0
5 hours ago, XEKEX said:

this is why it's not a good practice because ipairs , pairs and every built-in functions in lua that deals with tables start the iteration by default at index 1 
image.thumb.png.41d241fe770b93d8f92502f4ea02de40.png --> here is an example

It is only for table readability when using print. If indexing in the game starts at zero then i do want the index of the table,(for readability when using print) to start at zero. Not that it firsts shows index 1 and then all at the end shows index 0. It's a simple thing for the sake of not getting any confusion.

I would not do it if i needed to use the table for something else.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.