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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T01:08:46+00:00 2026-05-30T01:08:46+00:00

The output for the below script is: AD[1] = [variable not found] AD[‘2’] =

  • 0

The output for the below script is:

AD[1] = [variable not found]
AD['2'] = bar

How can I modify the function getfield to return a value for v for both cases?

function getfield (f)
  local v = _G    
  for w in string.gfind(f, "[%w_]+") do
    v = v[w]
  end
 return v
end

AD = {[1] = 'foo', ['2'] = 'bar'}
data = {"AD[1]","AD['2']"}

for i,line in ipairs(data) do
  s = getfield(line)
  if s then
        print(line .. " = " .. s)
  else 
    print(line .. " = [variable not found]")
  end
end

UPDATE:
I’m 90% sure, this is going to work for me:

function getfield (f)
  local v = _G    
    for w in string.gfind(f, "['%w_]+") do
      if (string.find(w,"['%a_]")==nil) then
        w = loadstring('return '..w)()
      else 
        w = string.gsub(w, "'", "") 
      end
      v=v[w]
  end
  return v
end
  • 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-30T01:08:47+00:00Added an answer on May 30, 2026 at 1:08 am

    This happens to work

    function getfield (f)
      local v = _G    
      for w in string.gfind(f, "['%w_]+") do
        local x = loadstring('return '..w)()
        print(w,x)
        v = v[x] or v[w]
      end
     return v
    end
    
    AD = {[1] = 'foo', ['2'] = 'bar'}
    data = {"AD[1]","AD['2']"}
    
    for i,line in ipairs(data) do
      s = getfield(line)
      if s then
            print(line .. " = " .. s)
      else 
        print(line .. " = [variable not found]")
      end
    end
    

    but it’s pretty fragile.

    Note that I added ' to the pattern.

    The difficulty is that sometimes w is a string representing a name (key), and sometimes it’s a string representing a number. In the second case it needs to be converted from string to number. But you need the context or some syntax to decide.

    Here’s the kind of fragility I mean:

    >     data = {"math[pi]","AD['2']"}
    >     
    >     for i,line in ipairs(data) do
    >>       s = getfield(line)
    >>       if s then
    >>             print(line .. " = " .. s)
    >>       else 
    >>         print(line .. " = [variable not found]")
    >>       end
    >>     end
    math    table: 0x10ee05100
    pi  nil
    math[pi] = 3.1415926535898
    AD  table: 0x10ee19ee0
    '2' 2
    AD['2'] = bar
    
    
    > pi = 3
    > math[3] = 42
    >     data = {"math[pi]","AD['2']"}>
    >     for i,line in ipairs(data) do
    >>       s = getfield(line)
    >>       if s then
    >>             print(line .. " = " .. s)
    >>       else 
    >>         print(line .. " = [variable not found]")
    >>       end
    >>     end
    math    table: 0x10ee05100
    pi  3
    math[pi] = 42
    AD  table: 0x10ee19ee0
    '2' 2
    AD['2'] = bar
    

    math[pi] is unchanged, but getfield interprets pi in the global context and gets 3 so the wrong field of math is returned.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My LINQ query is not producing the expected output below. Basically, it's the sum
Im not sure how to output MySQL data into formats below. (eg: timelist, usersex,
any idea how i can get the code below to produce this output? 1
I have this script below which i found on SO to generate pagination and
My batch file terminates prematurely after I assign the first environmental variable (script output
The code below is streaming the twitter public timeline for a variable which output
I have a python script which outputs lots of data, sample is as below.
I'm having some problems trying to get the code below to output the data
The snippet below is generating weird output: for s in servers: vo = ss.getServerVO(s)
I'm trying to add new output column using synchronous custom data flow component(below is

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.