indexing of new created tables

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
9 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

indexing of new created tables

temp212
Why a new table needs to be put in parentheses to index it?
a =  {4, 3, 2, 1} [2]   --error
a = ({4, 3, 2, 1})[2]   --ok
at the same time length operator # works either way.


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Charles Heywood

The # symbol is an operator, while [....] is not. While I'm not sure why () is required, it's important to observe that *all* operators don't need ().


On Sun, Jul 23, 2017, 08:17 <[hidden email]> wrote:
Why a new table needs to be put in parentheses to index it?
a =  {4, 3, 2, 1} [2]   --error
a = ({4, 3, 2, 1})[2]   --ok
at the same time length operator # works either way.


--
--
Ryan <[hidden email]>
Software Developer / System Administrator
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

szbnwer@gmail.com
+1 that would be nice :)

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Michael Rosenberg
In reply to this post by temp212
Take a look at the grammar specification:

prefixexp ::= var | functioncall | ‘(’ exp ‘)’
var ::=  Name | prefixexp ‘[’ exp ‘]’ | prefixexp ‘.’ Name
exp ::=  nil | false | true | Numeral | LiteralString | ‘...’ | functiondef | prefixexp | tableconstructor | exp binop exp | unop exp
tableconstructor ::= ‘{’ [fieldlist] ‘}’

So the `[2]` indexing operation must be part of a var symbol. That means that whatever precedes it must be a prefixexp. The tableconstructor {1,2,3} is a valid exp, but prefixexp can only take exps that are enclosed in parens. It's not very enlightening, but that's why you need parens to index into a tableconstructor—because the spec says so.

> On Jul 23, 2017, at 8:07 AM, [hidden email] wrote:
>
> Why a new table needs to be put in parentheses to index it?
> a =  {4, 3, 2, 1} [2]   --error
> a = ({4, 3, 2, 1})[2]   --ok
> at the same time length operator # works either way.
>
>


Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

szbnwer@gmail.com
then just
var ::=  Name | tableconstructor ‘[’ exp ‘]’ | prefixexp ‘[’ exp ‘]’ |
prefixexp ‘.’ Name
:D

i think the complexity of implementation is the real question here

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Thomas Jericke
On 25.07.2017 06:17, [hidden email] wrote:
then just
var ::=  Name | tableconstructor ‘[’ exp ‘]’ | prefixexp ‘[’ exp ‘]’ |
prefixexp ‘.’ Name
:D

i think the complexity of implementation is the real question here

You can look at my existing implementation for Lua 5.2

http://lua-users.org/wiki/ThomasJericke
http://lua-users.org/files/wiki_insecure/power_patches/5.2/optbrackets-5.2.2.patch

Second time in a short while I post this, but this time it is really appropriate.
--
Thomas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Dirk Laurie-2
2017-07-25 6:58 GMT+02:00 Thomas Jericke <[hidden email]>:

> On 25.07.2017 06:17, [hidden email] wrote:
>
> then just
> var ::=  Name | tableconstructor ‘[’ exp ‘]’ | prefixexp ‘[’ exp ‘]’ |
> prefixexp ‘.’ Name
> :D
>
> i think the complexity of implementation is the real question here
>
> You can look at my existing implementation for Lua 5.2
>
> http://lua-users.org/wiki/ThomasJericke
> http://lua-users.org/files/wiki_insecure/power_patches/5.2/optbrackets-5.2.2.patch
>
> Second time in a short while I post this, but this time it is really
> appropriate.

Thanks for doing so. I remembered somone had done it but forgot who.

Please consider putting a short description of it on

   http://lua-users.org/wiki/LuaPowerPatches

(That's where I looked in vain.)

Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Thomas Jericke
On 25.07.2017 08:33, Dirk Laurie wrote:
2017-07-25 6:58 GMT+02:00 Thomas Jericke [hidden email]:
On 25.07.2017 06:17, [hidden email] wrote:

then just
var ::=  Name | tableconstructor ‘[’ exp ‘]’ | prefixexp ‘[’ exp ‘]’ |
prefixexp ‘.’ Name
:D

i think the complexity of implementation is the real question here

You can look at my existing implementation for Lua 5.2

http://lua-users.org/wiki/ThomasJericke
http://lua-users.org/files/wiki_insecure/power_patches/5.2/optbrackets-5.2.2.patch

Second time in a short while I post this, but this time it is really
appropriate.
Thanks for doing so. I remembered somone had done it but forgot who.

Please consider putting a short description of it on

   http://lua-users.org/wiki/LuaPowerPatches

(That's where I looked in vain.)

I will have to check my patch for meeting the guidelines first.
Also I try to figure out in which order this side is ordered to know where to add my patch. :-J
--
Thomas
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: indexing of new created tables

Dirk Laurie-2
2017-07-25 8:52 GMT+02:00 Thomas Jericke <[hidden email]>:

> On 25.07.2017 08:33, Dirk Laurie wrote:
> Please consider putting a short description of it on
>
>    http://lua-users.org/wiki/LuaPowerPatches
>
> (That's where I looked in vain.)
>
> I will have to check my patch for meeting the guidelines first.
> Also I try to figure out in which order this side is ordered to know where
> to add my patch. :-J

That's easiest of all. Newest patches go on the top, except if your patch
only applies to an older version than the latest, in which case it goes
to the top of that version's patches.

Loading...