Announcement

Collapse
No announcement yet.

Using array as function parameter

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Imagine Programming
    replied
    ipairs fetches the array-like indexes in an array (numeric) and pairs iterates over all keys.

    - http://www.luafaq.org/#T1.10

    Google is your best friend.

    Leave a comment:


  • Armanyes
    replied
    Thanks both of you. I have a little question: What is the difference between pairs and ipairs?

    Leave a comment:


  • Imagine Programming
    replied
    Yes, but you then cannot store the multiple values in a table beforehand, unless you use unpack.

    Leave a comment:


  • webultra
    replied
    This one also works

    PHP Code:
    function sample(...)
        for 
    keyvalue in ipairs (arg) do
            
    Dialog.Message("Notice"valueMB_OKMB_ICONINFORMATIONMB_DEFBUTTON1);
        
    end
    end
    sample
    (1,2,3,4,5,6

    Leave a comment:


  • Armanyes
    replied
    Thank you very much

    Leave a comment:


  • Imagine Programming
    replied
    Code:
    function sample(pars)
    	if(type(pars) ~= "table")then
    		error("argument #1: table expected, got " .. type(pars), 2);
    	end
    
    	for key, value in pairs(pars) do
    		Dialog.Message("Notice", value, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    	end 
    end
    
    local t = {"Hello", "World!!", "Tables", "are", "awesome."};
    sample(t);

    Leave a comment:


  • Armanyes
    started a topic Using array as function parameter

    Using array as function parameter

    I used this code to do the job:

    Code:
    function sample(par1, par2, par3)
    	pars = {par1, par2, par3};
    	for key, value in pairs (pars) do
    	    Dialog.Message("Notice", value, MB_OK, MB_ICONINFORMATION, MB_DEFBUTTON1);
    	end 
    end
    And it works fine but I think there must be a better way to do this, because parameters may increase or decrease during using the function.

    Any one to help out
Working...
X