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

  • Home
  • SEARCH
  • 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 8857853
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:41:33+00:00 2026-06-14T14:41:33+00:00

I need a piece of code in lua language that can find sequential items

  • 0

I need a piece of code in lua language that can find sequential items in an array that the number of item in the group exceeds a specific nubmer. Example:if I have the array(the numbers won’t be in the right order, randomly distributed)->( 2,5,9,10,11,21,23,15,14,12,22,13,24 ) ; there are two sequential groups (9,10,11,12,13,14,15) and (21,22,23,24 ) . I want the first group to be found if the specific number say (4) or more, or I can get the two groups if the number is (3) or less for example.
thanks

  • 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-06-14T14:41:33+00:00Added an answer on June 14, 2026 at 2:41 pm

    The logical way would seem to be to reorder the table and look for gaps in the sequences.

    function table.copy(t)
        local t2 = {}
        for k,v in pairs(t) do
            t2[k] = v
        end
        return t2
    end
    
    function groups(org, cnt)
        --  Returns a table containing tables containing the groups found
        local res = {}
        local group = {}
        tbl = table.copy(org) -- Prevent reordering of Original Table
        table.sort(tbl)
        local last = nil
        for _,val in ipairs(tbl) do
            if last and last + 1 ~= val then
                if #group >= cnt then
                    table.insert(res,group)
                end
                group = {}
            end
            table.insert(group,val)
            last = val
        end
        if #group >= cnt then
            table.insert(res,group)
        end
        return res
    end
    local org = { 2,5,9,10,11,21,23,15,14,12,22,13,24 }
    local result = groups(org,3)
    print('Number of Groups',#result)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need a piece of code with which I can extract the substrings that
I am in need of a piece of code that can detect if a
I need to define some common piece of code that can be invoked from
I need to change a piece of code which includes this: string.indexOf(bc) How can
I need to translate this piece of code from Perl to Lua open(FILE, '/proc/meminfo');
I have a piece of code in vb. I need to convert array of
I have a piece of code that will sometimes need to be triggered by
I need to put together a piece of code that parses a possibly large
I have this piece of code that currently almost does what I need. It
I need a piece of code that basically lets me specify the length of

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.