Bug With HaveItem()

Report any Hollywood bugs here
Post Reply
PEB
Posts: 569
Joined: Sun Feb 21, 2010 1:28 am

Bug With HaveItem()

Post by PEB »

Hi Andreas,

HaveItem() seems to have trouble recognizing table items assigned to string indices. Here is some example code:

Code: Select all

TestTable1$={}
TestTable2$={}
TestTable1$["Test"]=TestTable2$
;TestTable1$[5]=TestTable2$

DebugPrint(HaveItem(TestTable1$, "Test"))
;DebugPrint(HaveItem(TestTable1$, 5))
Thankfully, RawGet() still recognizes all the table items as it should.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Bug With HaveItem()

Post by airsoftsoftwair »

It's not a bug, it's a feature :) You see this behaviour because you're using an index string that contains upper-case characters. When assigning table values using string indices, Hollywood actually does distinguish between upper and lower case characters, so the following lines all reference different table indices:

Code: Select all

t = {}
t["test"] = 1
t["Test"] = 2
t["TEST"] = 3
DebugPrint(t["test"], t["Test"], t["TEST"])   ; prints 1 2 3
This is an undocumented feature that has been there since Hollywood 2.0. The rationale behind it is that there are some cases where it might actually come in handy to have the possibility to access case sensitive variables. When using the normal dot assignment syntax, however, Hollywood will never be case sensitive, i.e.

Code: Select all

t = {}
t.test = 1
t.Test = 2
t.TEST = 3
DebugPrint(t.test, t.Test, t.TEST)   ; prints 3 3 3
Internally, Hollywood will treat all those assignments as lower-case strings. Only when using string indices does Hollywood use case sensitivity.

So to fix your script, just do:

Code: Select all

TestTable1$={}
TestTable2$={}
TestTable1$["test"]=TestTable2$
DebugPrint(HaveItem(TestTable1$, "Test"))
HaveItem() only allows you to access lower-case string indices whereas RawGet() also allows access to mixed-case or upper-case string indices, hence the name RawGet().
User avatar
lazi
Posts: 625
Joined: Thu Feb 24, 2011 11:08 pm

Re: Bug With HaveItem()

Post by lazi »

As I had a similar problem with HaveItem(), made a case-sensitive replacement :)

Code: Select all

Function HaveItem(a,b)
	   If RawGet(a,b)=Nil Then Return(False)
   	   Return(True)
EndFunction
Post Reply