Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Writing Code into a string and then extracting it to be run...a table class

edited February 2014 in Questions Posts: 63

This may be answered elsewhere but I'm not sure what I'm looking for, so apologies if it is and please direct me if possible...Anyway...

Is it possible to write code into a string and then in whatever way strip off the stringness of the string and run the code?

Comments

  • BriarfoxBriarfox Mod
    edited February 2014 Posts: 1,542

    @TheAbstractMan look up loadstring().

    function setup()
    str = "function hello() print('Hello World') end"
        loadstring(str)()
        hello()    
    end
    
    
  • Thank you @Briarfox, will do.

  • BriarfoxBriarfox Mod
    Posts: 1,542

    @TheAbstractMan I edited the above post. Thought I could get some code in before you read it :)

  • That's brilliant...I'm just trying to look up loadstring() and can't find it in the codea reference or lua reference and only passing reference in the Programming in Lua book, so the above is priceless...and simple! Many thanks.

  • BriarfoxBriarfox Mod
    Posts: 1,542

    @TheAbstractMan You can do it this way which allows you to set the function/table or whatever variable.

    function setup()
    str = "return function() print('Hello World') end"
        newFunc = loadstring(str)()
        newFunc()    
    end
  • Thank you again @Briarfox. I'm trying to write a table class as I'm sick of constantly setting up loops for multidimensional tables, but immediately ran into difficulties since it's going to involve a variadic function call(no problem) but them I need a system to set up a variable amount of loops from the information passed...My brain started whispering that building up a string and then implementing that might be the way forward...Will see, probably fall flat on my face but it's all learning.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @TheAbstractMan - couldn't you use recursion?

  • edited February 2014 Posts: 63

    Hi @Ignatz. I'm not sure what you mean...now on p57 of Programming in Lua, looking up recursive functions...but not really giving me clarity...

    My thinking is as follows (probably naive)...if I want to set up an dimensional table then I'll need to pass on the dimension information (which is going to vary case by case), so for example lets say i want to create a 3x3x9 table so I'd do something along the lines of

    Note this is not proper code...
    
    newtable=tableCreate (3,3,9)
    
    tableCreate=class()
    
    function tablecreate:init(...)
    dim={...}
    self={}
    

    so I can get the number of dimensions from #dim but as #dim varies with each class call, I'll need a different amount of iterative loops to create each unique table...for a specific request i.e. the example given above I'll need for a=1,dim[1] do self[a]={}; for b=1,dim[2] do self[a][b]={}; for c=1,dim[3] do self[a][b][c]=0

    So obviously (in my thinking) for an n-th dimensional table it would need n nested loops...so my logic leads me to the question how do I set up a variable amount of loops of the form "for BLAH = 1,dim[BLEE] do self BLUE={}" where BLAH has to change its variable, BLEE it's number and BLUE has to grow from [a] to [a][b] to [a][b][c]...etc...

    Thus the string question...Having just spent 2 hours reading the string library in Programming for Lua I have now almost succeeded but also learnt a lot about string commands so am happy to abandon my naive mode of thinking if there is a shorter alternative way. As always, I would be very grateful for the chance of enlightenment.

  • Posts: 1,976

    @TheAbstractMan Recursion is calling a function from within itself. In this case, in your table conversion function, you could call your own function to search any new table it finds.

  • Oh, okay yep I get that @SkyTheCoder. Thank you. I had considered something along those lines (didn't realise that it was "recursion"...obvious now!) but with regard to the manner in which I am familiar with setting up multi-dimensional table arrays I couldn't see how this would work predominantly because of the "growth" required with regard to the BLUE mentioned above.

  • edited February 2014 Posts: 63

    With regard to the above description, this is my construction so far... obviously a work in progress.

    function setup()
    newtable=createTable(3,4,5)
    end
    
    createTable = class()
    
    Code="abcdefghijkmnopqrstuvwxyz"
    
    function createTable:init(...)
        self={}
        local  dim = {...} -- so I now know the dimensions and the number
                                  -- of dimensions #dim
        str = ""
        command = "for BLAH=1,dim[BLUE], do selfBLEE={} "
        command = string.rep(command,#dim)   
        lastLine = "end "
        Lastline = ""
        for z=1,#dim do
        str = str .. "[" .. string.sub(Code,z,z) .."]"
        Lastline = Lastline..lastLine
        print (str)
        print (Lastline)
        end
        end
    

    I'm just experimenting with creating the "growth" at the moment with a #dim sized loop

  • IgnatzIgnatz Mod
    edited February 2014 Posts: 5,396

    @TheAbstractMan - I was thinking of something like this

    function setup()
        t={2,4,3,4}
        tbl=CreateTable(t)
        --test print for 4D table, should print 0 for all items without errors
        for i=1,t[1] do
            for j=1,t[2] do
                for k=1,t[3] do
                    for m=1,t[4] do
                        print(i,j,k,m,tbl[i][j][k][m])
                    end
                end
            end
        end
    end
    
    function CreateTable(t)
        local tbl={}
        tbl=AddDim(tbl,t,0)
        return tbl    
    end
    
    function AddDim(tbl,t,n)
        n=n+1
        for i=1,t[n] do
            --add next dimension if required
            if #t>n then tbl[i]={} AddDim(tbl[i],t,n)
            --otherwise this is the end, just initialise
            else tbl[i]=0
            end
        end
        return tbl
    end
    
  • IgnatzIgnatz Mod
    Posts: 5,396

    @TheAbstractMan - edited my code above, I hope it works now

  • edited February 2014 Posts: 63

    @IgnatzI can see your thinking there...you're not passing tbl back into the AddDim function but tbl[i]. When I was considering this type of function loop I hadn't thought about that, I was just passing the equivalent of tbl back through and then finding myself in the same situation that I had done prior to the consideration of the function loop with the necessity of modifying the contents of the loop again.
    Having said this, as you pass tbl[i] back through, what is the scope of i? Will the next setting of i not actually create a different i and even if it is the same will this not just only create the diagonal of a table...Sorry I know you said it is a work in progress...and I've just heard something ping through! So this should probably come before the previous comment.

  • IgnatzIgnatz Mod
    Posts: 5,396

    @TheAbstractMan - i is local to each function. Loop variables are automatically local to their loop.

  • @Ignatz, sorry about the delay in responding, just analysing your code. Magnificent as always...Thought I had some questions, but upon writing them, clarified them for myself. Many thanks...will leave my Frankenstein method behind!

  • edited February 2014 Posts: 63

    For anyone browsing...after @Ignatz

    createTable = class()
    
    function createTable:init(...)
        self.dim={...}
        local tbl={}
        tbl=AddDim(tbl,self.dim,0)
        self.T = tbl
    end
    
    function AddDim(tbl,t,n)
        n=n+1
        for i=1,t[n] do
            --add next dimension if required
            if #t>n then tbl[i]={} AddDim(tbl[i],t,n)
            --otherwise this is the end, just initialise
            else tbl[i]=0
            end
        end
        return tbl
    end
    

    call a class function using xxxx = createTable (dimensions) where dimension of 2x3x5 would be 2,3,5 etc Instances of class would be xxxx.dim - returns dimensions, xxxx.T - returns table of dimensions...

Sign In or Register to comment.