How many table accesses caused by this expression?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

How many table accesses caused by this expression?

孙世龙 sunshilong
Hi, list

How many table accesses caused by this expression:

zip = company and company.director and
company.director.address and
company.director.address.zipcode

As per the book(programming in Lua, 4th edition), the answer is 6.
Why?
For 0+1+2+3?

Why "company" does not perform a table access?
Could somebody shed some light on this question?

Best regards.
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How many table accesses caused by this expression?

Robert Burke
If "company" is a local variable, you have its value on the stack. If it is global, you will need a table lookup to find it.

On Sun, Oct 11, 2020, 18:20 孙世龙 sunshilong <[hidden email]> wrote:
Hi, list

How many table accesses caused by this expression:

zip = company and company.director and
company.director.address and
company.director.address.zipcode

As per the book(programming in Lua, 4th edition), the answer is 6.
Why?
For 0+1+2+3?

Why "company" does not perform a table access?
Could somebody shed some light on this question?

Best regards.
Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How many table accesses caused by this expression?

孙世龙 sunshilong
According to the context of the book, "company" is a global library,
like "math", "string" and so on.

For details, see <<programming in Lua, 4th edition>> page 39.

On Sun, Oct 11, 2020 at 6:06 PM Robert Burke <[hidden email]> wrote:

>
> If "company" is a local variable, you have its value on the stack. If it is global, you will need a table lookup to find it.
>
> On Sun, Oct 11, 2020, 18:20 孙世龙 sunshilong <[hidden email]> wrote:
>>
>> Hi, list
>>
>> How many table accesses caused by this expression:
>>
>> zip = company and company.director and
>> company.director.address and
>> company.director.address.zipcode
>>
>> As per the book(programming in Lua, 4th edition), the answer is 6.
>> Why?
>> For 0+1+2+3?
>>
>> Why "company" does not perform a table access?
>> Could somebody shed some light on this question?
>>
>> Best regards.
>> Sunshilong
Reply | Threaded
Open this post in threaded view
|

Re: How many table accesses caused by this expression?

孙世龙 sunshilong
>>
>> According to the context of the book, "company" is a global library,
>> like "math", "string" and so on.
>>
>> For details, see <<programming in Lua, 4th edition>> page 39.
>>
>Alright. After reviewing that section of the book, I think that it may
>either regard company as a local variable or regard the fact that
>global variables are looked up in a table as a detail irrelevant to
>the topic. Not sure which.
If I understand you correctly, the answer is 10 (i.e. 1+2+3+4) if it's
a global variable, whereas the answer is 6(i.e. 0+1+2+3) if it's a
local variable.
Am I right?
Please let me know if I miss something.

Thank you for your attention to my question.

Best regards
Sunshilong

On Sun, Oct 11, 2020 at 9:37 PM Robert Burke <[hidden email]> wrote:

>
> On Sun, Oct 11, 2020 at 8:44 PM 孙世龙 sunshilong <[hidden email]> wrote:
> >
> > According to the context of the book, "company" is a global library,
> > like "math", "string" and so on.
> >
> > For details, see <<programming in Lua, 4th edition>> page 39.
> >
> Alright. After reviewing that section of the book, I think that it may
> either regard company as a local variable or regard the fact that
> global variables are looked up in a table as a detail irrelevant to
> the topic. Not sure which.