Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6636707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T23:13:11+00:00 2026-05-25T23:13:11+00:00

My assignment is how to do a for loop. I have figured it out

  • 0

My assignment is how to do a for loop. I have figured it out in terms of numbers but cannot figure it out in terms of names. I would like to create a for loop that runs down a list of names. Following is what I have so far:

names = {'John', 'Joe', 'Steve'}
for names = 1, 3 do
  print (names)
end

I have tried some other things but it just doesn’t work, the terminal always just lists 1, 2, 3… What I am doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T23:13:11+00:00Added an answer on May 25, 2026 at 11:13 pm

    Your problem is simple:

    names = {'John', 'Joe', 'Steve'}
    for names = 1, 3 do
      print (names)
    end
    

    This code first declares a global variable called names. Then, you start a for loop. The for loop declares a local variable that just happens to be called names too; the fact that a variable had previously been defined with names is entirely irrelevant. Any use of names inside the for loop will refer to the local one, not the global one.

    The for loop says that the inner part of the loop will be called with names = 1, then names = 2, and finally names = 3. The for loop declares a counter that counts from the first number to the last, and it will call the inner code once for each value it counts.

    What you actually wanted was something like this:

    names = {'John', 'Joe', 'Steve'}
    for nameCount = 1, 3 do
      print (names[nameCount])
    end
    

    The [] syntax is how you access the members of a Lua table. Lua tables map “keys” to “values”. Your array automatically creates keys of integer type, which increase. So the key associated with “Joe” in the table is 2 (Lua indices always start at 1).

    Therefore, you need a for loop that counts from 1 to 3, which you get. You use the count variable to access the element from the table.

    However, this has a flaw. What happens if you remove one of the elements from the list?

    names = {'John', 'Joe'}
    for nameCount = 1, 3 do
      print (names[nameCount])
    end
    

    Now, we get John Joe nil, because attempting to access values from a table that don’t exist results in nil. To prevent this, we need to count from 1 to the length of the table:

    names = {'John', 'Joe'}
    for nameCount = 1, #names do
      print (names[nameCount])
    end
    

    The # is the length operator. It works on tables and strings, returning the length of either. Now, no matter how large or small names gets, this will always work.

    However, there is a more convenient way to iterate through an array of items:

    names = {'John', 'Joe', 'Steve'}
    for i, name in ipairs(names) do
      print (name)
    end
    

    ipairs is a Lua standard function that iterates over a list. This style of for loop, the iterator for loop, uses this kind of iterator function. The i value is the index of the entry in the array. The name value is the value at that index. So it basically does a lot of grunt work for you.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an assignment to find the minimum, maximum, and average of numbers that
I'm trying to figure out if i have the general idea behind the assignment
I've been trying to figure out this problem. I have an assignment to make
I have an assignment to create a GUI using MATLAB GUIDE and am having
My head is getting twisted trying to figure this out. The assignment is to
I have a for loop that writes the results of a query into a
I have this loop that parses machine code and emulates the instructions as if
So I have an assignment that is due later tonight and I'm stuck on
I am working on an assignment with polymorphism and have followed online tutorials that
For my homework assignment, I have a network of Nodes that are passing messages

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.