Unpack usage Problems

Source: Internet
Author: User
Tags unpack
  • Symptom

Lua supports an indefinite number of parameters, which can be transmitted through... and unpack. Read a piece of code

 1 local function arg2(...) 2     print('****************') 3     print(unpack(arg)) 4     for k, v in pairs(arg) do 5         print(k, v) 6     end 7     print(arg[1], arg[2], arg[3]) 8     print('++++++++++++++++') 9 end10 arg2('a', nil, 'c')11 arg2(nil, 'b', 'c')12 arg2(nil, 'b', nil)

In fact, we expect to pass in three parameters. However, due to some situations, some parameters have the same value as nil. Check the execution result.

We found that the first two were the same as expected, but the third unpack did not get the content. It was an empty string, but the content still exists when it was printed one by subscript, this will inevitably cause the function execution result to be inconsistent with the input parameter.

  • Explanation

Unpack is described as follows, which is taken from the specified range in sequence. If it is not specified, the length ranges from 1 to l are determined by the length operator.

I and j are not provided in the above Code, which are processed by default. The specified range is as follows:

 1 local function arg2(...) 2     print('****************') 3     print(unpack(arg, 1, 3)) 4     for k, v in pairs(arg) do 5         print(k, v) 6     end 7     print(arg[1], arg[2], arg[3]) 8     print('++++++++++++++++') 9 end10 arg2('a', nil, 'c')11 arg2(nil, 'b', 'c')12 arg2(nil, 'b', nil)

The execution is as follows:

This is the same as expected. Unfortunately, we don't always know the length. The result is incorrect when it is not specified. Conjecture is related to the result of # operation. The document description is

The key is the remaining part. If the first element is nil, the length is 0. If there is a "hole" between non-nil values, the length is not determined.

  • Code

The following is the unpack code.

 1 static int luaB_unpack (lua_State *L) { 2   int i, e, n; 3   luaL_checktype(L, 1, LUA_TTABLE); 4   i = luaL_optint(L, 2, 1); 5   e = luaL_opt(L, luaL_checkint, 3, luaL_getn(L, 1)); 6   n = e - i + 1;  /* number of elements */ 7   if (n <= 0) return 0;  /* empty range */ 8   luaL_checkstack(L, n, "table too big to unpack"); 9   for (; i<=e; i++)  /* push arg[i...e] */10     lua_rawgeti(L, 1, i);11   return n;12 }

The process for solving #

 1 static int unbound_search (Table *t, unsigned int j) { 2   unsigned int i = j;  /* i is zero or a present index */ 3   j++; 4   /* find `i' and `j' such that i is present and j is not */ 5   while (!ttisnil(luaH_getnum(t, j))) { 6     i = j; 7     j *= 2; 8     if (j > cast(unsigned int, MAX_INT)) {  /* overflow? */ 9       /* table was built with bad purposes: resort to linear search */10       i = 1;11       while (!ttisnil(luaH_getnum(t, i))) i++;12       return i - 1;13     }14   }15   /* now do a binary search between them */16   while (j - i > 1) {17     unsigned int m = (i+j)/2;18     if (ttisnil(luaH_getnum(t, m))) j = m;19     else i = m;20   }21   return i;22 }23 24 25 /*26 ** Try to find a boundary in table `t'. A `boundary' is an integer index27 ** such that t[i] is non-nil and t[i+1] is nil (and 0 if t[1] is nil).28 */29 int luaH_getn (Table *t) {30   unsigned int j = t->sizearray;31   if (j > 0 && ttisnil(&t->array[j - 1])) {32     /* there is a boundary in the array part: (binary) search for it */33     unsigned int i = 0;34     while (j - i > 1) {35       unsigned int m = (i+j)/2;36       if (ttisnil(&t->array[m - 1])) j = m;37       else i = m;38     }39     return i;40   }41   /* else must find a boundary in hash part */42   else if (t->node == dummynode)  /* hash part is empty? */43     return j;  /* that is easy... */44   else return unbound_search(t, j);45 }

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.