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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T09:53:43+00:00 2026-06-18T09:53:43+00:00

I have 2 functions in Lua which create a dictionary table and allow to

  • 0

I have 2 functions in Lua which create a dictionary table and allow to check if a word exists:

local dictTable = {}
local dictTableSize = 0

function buildDictionary()
    local path = system.pathForFile("wordlist.txt")
    local file = io.open( path, "r")
    if file then
        for line in file:lines() do
            dictTable[line] = true
            dictTableSize = dictTableSize + 1
        end      
        io.close(file)
    end
end

function checkWord(word)
    if dictTable[word] then
        return(true)
    else
        return(false)
    end
end

Now I want to be able to generate a couple of random words. But since the words are the keys, how can I pick some, given the dictTableSize.

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-18T09:53:44+00:00Added an answer on June 18, 2026 at 9:53 am

    Just add a numerical index for each word to the dictionary while loading it:

    function buildDictionary()
        local path = system.pathForFile("wordlist.txt")
        local file = io.open( path, "r")
        if file then
            local index = 1
            for line in file:lines() do
                dictTable[line] = true
                dictTable[index] = line
                index = index + 1
            end      
            io.close(file)
        end
    end
    

    Now you can get a random word like this:

    function randomWord()
        return dictTable[math.random(1,#dictTable)]
    end
    

    Side note: nil evaluates to false in Lua conditionals, so you could write checkWord like this:

    function checkWord(word)
        return dictTable[word]
    end
    

    Another side note, you’ll get less polution of the global namespace if you wrap the dictionary functionality into an object:

    local dictionary = { words = {} }
    
    function dictionary:load()
        local path = system.pathForFile('wordlist.txt')
        local file = io.open( path, 'r')
        if file then
            local index = 1
            for line in file:lines() do
                self.words[line] = true
                self.words[index] = line
                index = index + 1
            end      
            io.close(file)
        end
    end
    
    function dictionary:checkWord(word)
        return self.words[word]
    end
    
    function dictionary:randomWord()
        return self.words[math.random(1,#self.words)]
    end
    

    Then you can say:

    dictionary:load()
    dictionary:checkWord('foobar')
    dictionary:randomWord()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a function which exposes all of my required C++ functions to Lua,
Say I have a lua file: --functions.lua function testadd(a, b) return a+b end How
I have a lua animation variable which has a callback function used in an
I have a lua function which takes multiple parameters and returns as many values
in writing a scripting engine, I have functions like (psuedo-code) function is_whitespace?(char c){ return
I have two functions that return simple strings. Both are registered. $.views.helpers({ parseDate: function
I have two functions here function Preloader() {} Preloader.prototype = { init:function() { //
I have a C++ function, called lua_tcall, which extends the abilities of lua_pcall. When
I have a very simple little piece of Lua code, which I wrote while
I just got homework to create a lua menu which is as small as

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.