[03 Mar 2010] Pitfall: table access!

Contains all messages from the Hollywood mailing list between 01/2006 and 08/2012
Locked
nexus
Posts: 133
Joined: Sun Mar 07, 2010 11:54 am

[03 Mar 2010] Pitfall: table access!

Post by nexus »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 03 Mar 2010 16:16:02 -0000

I was hunting a bug in my code and, first, couldn't understand why it was a bug. Now, after tracing back the problem, i thought, i should write about it here, so others don't have to feel the same pain ;-)

The hollywood documentation says:

Code: Select all

You can access named elementes of a table also in two ways:

b = a["name"]
b = a.name

Both lines will assign the same value to b. The most common way to access and initialize named elements of a table is to use the dot method. *Please* *note* *that* *Hollywood* *does* *not* *distinguish* *between* *upper* *and* *lower* *case* *names*, so you could also access the elements above by using a.NAME or even a.nAmE"
Well, this is not quite true.

For example:

Code: Select all

test = {}
test.myName =  {}
Now see that:

Code: Select all

DebugPrint(RawGet(test,"myname")) --> True
DebugPrint(RawGet(test,"myName")) --> False

DebugPrint(test.myname) --> Table:
DebugPrint(test.myName) --> Table:
DebugPrint(test["myname"]) --> Table:
DebugPrint(test["myName"]) --> Error!
EVEN WORSE IS THIS:

Code: Select all

test2={}
test2["myName"] = "blabla"

DebugPrint(test2["myName"]) --> "blabla"
DebugPrint(test2["myname"]) --> Error!
AND ALSO:

Code: Select all

DebugPrint(test2.myname) --> Error!
DebugPrint(test2.myName) --> Error!
So you see, Hollywood *DOES* distinguish between lower and upper case letter. Furthermore, *IF* you have declared a table field *NOT* with the dot method, you cannot even access a table field with the dot mehtod *IF* the field was declared with some upper case letters!

Dunno, if this is a bug in the documentation or in Hollywood 4.5 :-)

regards, Tom
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

[03 Mar 2010] Re: Pitfall: table access!

Post by Bugala »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 03 Mar 2010 19:19:49 -0000

Great find.

Hope you post this to Hollywood Forum too.

I wonder too if this documentation problem or Hollywood problem.

For basically in example its all the time talking without "".

So it might be documentation unclarity, that Andreas didnt notice to mention that with "" it distinguishes between lower and upper case.
User avatar
Allanon
Posts: 732
Joined: Sun Feb 14, 2010 7:53 pm
Location: Italy
Contact:

[03 Mar 2010] Re: Pitfall: table access!

Post by Allanon »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Wed, 03 Mar 2010 22:24:08 -0000

I've noticed this behaviuor some times ago without reporting anywhere just because I was used to declare in lowercase when using quotes and read information with dot using uppercase, for example:

Code: Select all

mytable = {}
mytable["myname"] = "Fabio"

name = mytable.MyName
in this specific case Hollywood does not distinguish from upper case and lower case. Btw you are right in all your observations.

Regards, Fabio
User avatar
airsoftsoftwair
Posts: 5433
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

[05 Mar 2010] Re: Re: Pitfall: table access!

Post by airsoftsoftwair »

Note: This is an archived post that was originally sent to the Hollywood mailing list on Fri, 05 Mar 2010 22:35:21 +0100
I've noticed this behaviuor some times ago without reporting anywhere just because I was used to declare in lowercase when using quotes and read information with dot using uppercase, for example:

Code: Select all

mytable = {}
mytable["myname"] = "Fabio"

name = mytable.MyName
in this specific case Hollywood does not distinguish from upper case and lower case. Btw you are right in all your observations.
Yes, this is a very old known bug. I know about since Hollywood 2.0 but I haven't fixed it yet because it's very hard (involves a lot of reorganisation in the vm, garbage collector fixes etc etc.) Remember that Hollywood uses a hacked version of the lua vm and lua *does* distinguish between upper and lower case. I managed to change all instances of upper/lower case distinction except this single case: when using strings as table indices. In that case, Hollywood will be case sensitive, as you rightly observed. You can circumvent the problem by either using the dot operator only or using LowerStr() when using string indices, i.e.

Code: Select all

mytable[LowerStr("mYnAmE")] = "bla"
DebugPrint(mytable.myname)
DebugPrint(mytable.MYNAME)
DebugPrint(mytable.myNaMe)
etc.
If you use LowerStr() before setting the index, everything will be fine and Hollywood will be case insensitive again :)
Locked