Lua is weird...

min read

At 175 words per minute.

2024-09-16 Back to posts

Lua logo

Let me explain myself! 1-based indexing is not as bad as you think.

Lua is strange…

Lua has some quirks that can trip you up at first but once you get the hang of it, it can end up being intuitive.

Why is it strange?

Because of ‘1-Based Indexing’:

Unlike many other languages (C, Java, etc.) where arrays start at 0, Lua starts at 1.

It might seem odd at first, but it actually fits well with how we naturally count things.

In the code below, we have a list of fruits.

To any human, the FIRST item in the array would be number ONE.

Now, I don’t know how you we’re taught to count lists, but I start at 1 for the first item.

Lua’s way of handling arrays is different & strange compared to most programming languages but I got to admit, it has grown on me this weekend.

local fruits = {"apple", "bannana", "cherry"}
print("First Fruit:", fruits[1])

table.insert(fruits, 'blueberry')
print("Fourth Fruit:", fruits[4])


-- It can be intuitive...
for i = 1, #fruits do
  print("Fruit at position:",i,"is",fruits[i])
end

--output
--[[
First Fruit:    apple
Fourth Fruit:   blueberry
Fruit at position:      1       is      apple
Fruit at position:      2       is      bannana
Fruit at position:      3       is      cherry
Fruit at position:      4       is      blueberry
--]]

Conclusion

While Lua has some quirks, it is a fun language to learn and write.

Nick Stambaugh

Nick Stambaugh

Full Stack Engineer

Full Stack & Enterprise Application Engineer

Recent Posts

Thoughts on Pop!_OS

My thoughts on the Pop!_OS, as an engineer and gamer.

2025-03-31

Read more →

#Linux #OS #Tech

Why Micromanagement Kills Innovation

Micromanagement of workers, especially software engineers, is detrimental to innovation.

2025-03-19

Read more →

#Workplace #Leadership #Business

Adding Music To My Raylib App

Adding audio to a C application is easy with raylib!

2025-03-13

Read more →

#C #raylib #Coding #Low-Level Development #Tech #Music

Why I Love Houston As A Michigander

Houston is an amazing city to explore with a wide variety of interesting activities, culture, and food.

2025-02-22

Read more →

#Houston #Travel

What I've Learned about LINQ and MVC

An example of why I'm starting to love C# more than Go.

2025-1-19

Read more →

C# #.NET #Coding #Tech

[PART 1] Creating A SQL-Like Language in C#

Part 1 of creating a SQL-like language in C# for fun.

2025-1-3

Read more →

C# #.NET #Coding #Tech