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 6539587
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:52:35+00:00 2026-05-25T10:52:35+00:00

I have this table: no_table ={ {a=3, b=22, c=18, d=ABC}, {a=4, b=12, c=25, d=ABC},

  • 0

I have this table:

no_table ={
        {a="3", b="22", c="18", d="ABC"},
        {a="4", b="12", c="25", d="ABC"},
        {a="5", b="15", c="16", d="CDE"},
               }

This function:

function testfoo()
    i = 1
    while no_table[i] ~= nil do
        foo(no_table[i])
        i = i + 1
    end
end

and the foo function:

function foo(a,b,c,d)
    if no_table[i][4] ~= no_table[i-1][4]
        then
           print (a+b)
    elseif no_table[i][4] == no_table[i-1][4]
        then
           print (b+c)
    end
end

Can you help me find? :

  1. A way to be able to check if the two tables are or not equal (currently it gives me cannot index nil)

  2. A way to execute only the “print (b+c)” code if the equality is true, or if is not true then both “print (a+b)” first and “print (b+c) secondly without duplicating the code.

  • 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-25T10:52:36+00:00Added an answer on May 25, 2026 at 10:52 am

    Lots of problems I’m seeing in this. First, I’d never rely on i being set in an external function, it really should be a local variable and passed as a parameter if you need it. That said, you need to check if no_table[x] exists before trying to access no_table[x][y]. So, for foo you’d have:

    function foo(a,b,c,d)
        if not (no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4])
            then
               print (a+b)
        elseif no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4]
            then
               print (b+c)
        end
    end
    

    Also, for numbers in the table, if you want to do arithmetic, you need to remove the quotes:

    no_table ={
            {a=3, b=22, c=18, d="ABC"},
            {a=4, b=12, c=25, d="ABC"},
            {a=5, b=15, c=16, d="CDE"},
                   }
    

    Next, in testfoo, you’re passing a table, so you either need to split out the values of a, b, c, and d on your function call, or you can just pass the table itself and handle that in foo:

    function foo(t)
        if not (no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4])
            then
               print (t.a+t.b)
        elseif no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4]
            then
               print (t.b+t.c)
        end
    end
    

    This results in:

    > testfoo()
    25
    37
    31
    

    Edit: One final cleanup, since the conditions are the same, you can use an else rather than an elseif:

    function foo(t)
        if no_table[i] and no_table[i-1] and no_table[i][4] == no_table[i-1][4]
            then
               print (t.b+t.c)
        else
               print (t.a+t.b)
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table like this: <table id='inventory_table'> <tr id='no_items_avail'> <td> There are no
I have no idea how to do this. My markup: <table> <tr> <td id=colLeft>
I have this table in an Oracle DB which has a primary key defined
I have this table CREATE TABLE [dbo].[friend_blocked_list]( [subdomain] [varchar](50) NOT NULL, [un] [nvarchar](50) NOT
I have this table CREATE TABLE `codes` ( `id` int(11) unsigned NOT NULL AUTO_INCREMENT,
i have this table in UTF8 (collation is utf8_bin in case it matters). I
I have this table: CREATE TABLE `test` ( `ID` int(11) NOT NULL auto_increment, `text`
I have this table: fourn_category (id , sub) I am using this code to
I have this table: CREATE TABLE `point` ( `id` INT(11) NOT NULL AUTO_INCREMENT, `siteid`
I have this table: id feed_id ... Let's say that I have 500 rows

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.