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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:41:15+00:00 2026-05-28T00:41:15+00:00

I’m attempting to compare two tables of equal length with a function, since I

  • 0

I’m attempting to compare two tables of equal length with a function, since I don’t know of any other way to do so. However, with the following function, it fails to register, and I’ve no clue why. I’m hoping someone can provide insight to this problem or has a better way of comparing the two tables.

The tables are being populated with the following code:

str = "parameters determined by program (all digits)"
tableone = {}
for word in str:gmatch("%d") do table.insert(tableone,word) end

It’s identical for both tables, except, of course, the individual table names. The tables are being populated properly, and display properly when I print them. Here are two tables for the sake of this question:

tableone = {}
tabletwo = {}
for i=1,4 do table.insert(tableone, i) end
for i=1,4 do table.insert(tabletwo, i) end

Obviously, these two tables are going to be equal to each other. The function I wrote to compare the index tables is as follows:

function comparetables(t1, t2)
matchct = 0
 for i=1,#t1 do
    if t1[i] == t2[i] then
    matchct = matchct + 1
    end
if matchct == #t1 then
return true
end
end

I tried doing

print(comparetables(tableone,tabletwo))

to see if it’ll print “true” but no luck. To me, it seems like it should work without a problem. Yet it doesn’t. What am I missing? I’ve tried searching for something like a table.compare function that someone may have already written, but no such luck in finding one. Thanks for any suggestions!

Additional information:

The reason I’m comparing tables is for a mastermaind-type game. That means the following three rules must apply when comparing tables. The function I created was to just get me started, thinking I could work from there.

  1. When comparing the tables, if the numbers match, Ccount increases by 1.
  2. When comparing tables, if the value exists in a different index position, increment Pcount by 1

For example, with a table of values {1, 3, 3, 4} and a guess of {4, 4, 3, 1}, it would return Pcount of 2 (the one 4 and the 1) and a Ccount of 1 (the three in the third position). I think one of the hardest parts is going to be getting the comparison to recognize that the second 4 in the guess should not increment the Pcount at all.

  • 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-28T00:41:15+00:00Added an answer on May 28, 2026 at 12:41 am

    If you’re comparing objects that are more objecty than tabley in an Object oriented sense, then I’d look at implementing the functions in the lua OO way.

    Something like this should do the trick:

    GameState = {}
    GameState.mt = {}
    GameState.mt.fns = {}
    GameState.mt.__index =  GameState.mt.fns
    
    function GameState.new(a,b,c,d)
    -- TODO: put argument checks here...
      local retval = {}
      retval[1] = a
      retval[2] = b
      retval[3] = c
      retval[4] = d
      setmetatable(retval, GameState.mt)
      return retval
    end
    
    function GameState.mt.fns.print( self )
      print(" GameState: ", self[1], self[2], self[3], self[4] )
    end
    
    function GameState.mt.__tostring( self )
      return "GameState: "..self[1].." "..self[2].." "..self[3].." "..self[4]
    end
    
    function GameState.mt.__eq(self, other)
      -- Check it's actually a GameState, and all its bits match
      return getmetatable(other)==GameState.mt and
        (self[1] == other[1]) and 
        (self[2] == other[2]) and 
        (self[3] == other[3]) and 
        (self[4] == other[4])
    end
    

    Then you’d use it like this:

    state1 = GameState.new(1,2,3,4)
    state2 = GameState.new(1,2,3,4)
    
    print("State 1 is:")
    state1:print()
    
    print("State 2 is:")
    print(state2)
    
    print( "state1 == state2 : ", state1 == state2 )
    
    print( "Changing state 2") 
    state2[1]=2
    
    print( "state1 == state2 : ", state1 == state2 )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I would like to count the length of a string with PHP. The string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
Does anyone know how can I replace this 2 symbol below from the string
I need a function that will clean a strings' special characters. I do NOT
I want to construct a data frame in an Rcpp function, but when I
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.