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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T06:37:26+00:00 2026-06-18T06:37:26+00:00

My code is designed to read digits and turn them into Chinese pinyin: function

  • 0

My code is designed to read digits and turn them into Chinese pinyin:

function digitconverter (digit)
    if digit == "0" then
        cnumber = "ying2 "
    elseif digit == "1" then
        cnumber = "yi1 "
    elseif digit == "2" then
        cnumber = "er2 "
    elseif digit == "3" then
        cnumber = "san1 "
    elseif digit == "4" then
        cnumber = "si4 "
    elseif digit == "5" then
        cnumber = "wu3 "
    elseif digit == "6" then
        cnumber = "liu4 "
    elseif digit == "7" then
        cnumber = "qi1 "
    elseif digit == "8" then
        cnumber = "ba1 "
    elseif digit == "9" then
        cnumber = "jiu3 "
    end
    return cnumber
end

print("Enter a number to be converted:")

repeat
    strnumber = io.read("*line")
    number = tonumber(strnumber)
    if number ~= nil then
        continue = true
    else
        print("Invalid input. Please try again:")
        continue = false
    end
until continue == true
nlength = #strnumber

digits = {}
for d in string.gmatch(number, "%d") do
    digits[#digits + 1] = d
end

convnumber = ""
for d=1,nlength do
    convnumber = convnumber .. digitconverter(digits[d])
end
print(convnumber)

    io.read()

If I enter over 15 digits, it gets stuck (for lack of a better term). It WILL convert every digit, but the 16th will be random and the 17th and on will repeat another random one. I’ve been over it and I can’t figure out where it’s getting hung up. Thoughts?

  • 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-18T06:37:27+00:00Added an answer on June 18, 2026 at 6:37 am

    You’re iterating through the digits of number, not strnumber. The problem is when you get to too many digits, the string representation is going to be in scientific notation:

    strnumber = '1234567890123456789'
    number = tonumber(strnumber)
    print(number) --> 1.2345678901235e+018
    

    Side note: Lua is based on hashtables, which gives you (barring hash collisions) constant time lookup. So your digit converter can be simply written as a map:

    local digitmap = {
       ["0"] = "ying2 ",
       ["1"] = "yi1 ",
       ["2"] = "er2 ",
       ["3"] = "san1 ",
       ["4"] = "si4 ",
       ["5"] = "wu3 ",
       ["6"] = "liu4 ",
       ["7"] = "qi1 ",
       ["8"] = "ba1 ",
       ["9"] = "jiu3 ",
    }
    

    Also, building strings like this is very inefficient:

    for d=1,nlength do
       convnumber = convnumber .. digitconverter(digits[d])
    end
    

    You’re generating tons of intermediate strings, which requires a lot of allocations and produces a lot of garbage. It’s much faster to put all the values you need to concatenate into a table, then call table.concat. Another advantage is that you can specify a delimiter (right now, you’re hard coding the delimiter into your string table).

    Using those techniques, we can rewrite your code like this:

    local digitmap = {
       ['0'] = 'ying2',
       ['1'] = 'yi1',
       ['2'] = 'er2',
       ['3'] = 'san1',
       ['4'] = 'si4',
       ['5'] = 'wu3',
       ['6'] = 'liu4',
       ['7'] = 'qi1',
       ['8'] = 'ba1',
       ['9'] = 'jiu3',
    }
    
    print('Enter a number to be converted:')
    while true do
       strnumber = io.read('*line')
       if not strnumber:match('%D') then
          break
       end
       print('Invalid input. Please try again:')
    end
    
    local digits = {}
    for digit in string.gmatch(strnumber, '%d') do
       digits[#digits + 1] = digitmap[digit]
    end
    
    print(table.concat(digits, ' '))
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I've got this code designed to echo a list of people from a specific
I am dealing with an old code designed for iPhone OS 2.0. In this
Recently I tried to explain some poorly designed code to my project manager. All
The code below is designed to take a string in and remove any of
I have some PHP code that is designed to make a spreadsheet with formulas
Dealing with some legacy code and in trying to get a poorly designed database
I have my application designed with Repository pattern implemented and my code prepared for
The below code is designed to take a byte[] and several other things (see
I have the following jQuery code: var isUsernameAvailable = false; function CheckUsername(uname) { $.ajax({
This is my code: function geocode(){ var country_code ='us'; // var country_code =''; //

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.