Roku Developer Program

Join our online forum to talk to Roku developers and fellow channel creators. Ask questions, share tips with the community, and find helpful resources.
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 
TheEndless
Channel Surfer

Re: [Solved!] roAssociativeArray.append() with duplicate key

"EnTerr" wrote:
I notice that documentation has been updated to say
"ifAssociativeArray.Append(aa as Object)" wrote:
... If any key in aa are already associated with a value in this AssociativeArray, the current value is discarded and is replaced with the value in aa.
So it is behavior (B), the new AA overrides old AA content in case of overlapping keys. As any experienced developer would expect but now is in the RTFM and can be relied upon.

My gratitude goes to the forum's patron saint, RokuMarkn - thank you!

It's probably worth noting that case matters in this instance as well. So, if you have a key named "Test" and you append an AA with a key named "test", you'll end up with two separate keys.
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
renojim
Community Streaming Expert

Re: [Solved!] roAssociativeArray.append() with duplicate key

"TheEndless" wrote:
It's probably worth noting that case matters in this instance as well. So, if you have a key named "Test" and you append an AA with a key named "test", you'll end up with two separate keys.

That's interesting. Could that be considered a bug? i thought Associative Arrays were case insensitive by default, hence the SetModeCaseSensitive interface. Which one do you get if you Lookup "test"? How about "tESt"?

-JT
Roku Community Streaming Expert

Help others find this answer and click "Accept as Solution."
If you appreciate my answer, maybe give me a Kudo.

I am not a Roku employee.
0 Kudos
EnTerr
Roku Guru

Re: [Solved!] roAssociativeArray.append() with duplicate key

"renojim" wrote:
That's interesting. Could that be considered a bug?

I would consider this a bug - if it were true. But i am unable to reproduce the behavior:
BrightScript Debugger> d = {}
BrightScript Debugger> d["Test"] = "Test"
BrightScript Debugger> ?d
Test: Test

BrightScript Debugger> d2 = {}
BrightScript Debugger> d2["test"] = "test"
BrightScript Debugger> ? d2
test: test

BrightScript Debugger> d.append(d2)
BrightScript Debugger> ?d
test: test

PS. Case (in)sensitivity is a PITA, in general.
0 Kudos
squirreltown
Roku Guru

Re: roAssociativeArray.append() with duplicate keys

"TheEndless" wrote:

It's probably worth noting that case matters in this instance as well. So, if you have a key named "Test" and you append an AA with a key named "test", you'll end up with two separate keys.


So according to EnTerr's test it seems this is not the case unless you call setModeCaseSensitve? The key replaced the other.

Brightscript is case-insensitive except for the registry, right? Anything else?
Kinetics Screensavers
0 Kudos
TheEndless
Channel Surfer

Re: roAssociativeArray.append() with duplicate keys

Sorry, it seems case sensitivity is on when parsing a JSON string, so this is only true if you call the SetModeCaseSensitive() explicitly, or if your AA comes from a parsed JSON string.

For example:
BrightScript Debugger> test1 = ParseJSON("{" + Chr(34) + "Test" + Chr(34) + ": true}")
BrightScript Debugger> test2 = ParseJSON("{" + Chr(34) + "test" + Chr(34) + ": false}")
BrightScript Debugger> ?test1
Test: true

BrightScript Debugger> ?test2
test: false

BrightScript Debugger> test2.Append(test1)
BrightScript Debugger> ?test2
Test: true
test: false
My Channels: http://roku.permanence.com - Twitter: @TheEndlessDev
Instant Watch Browser (NetflixIWB), Aquarium Screensaver (AQUARIUM), Clever Clocks Screensaver (CLEVERCLOCKS), iTunes Podcasts (ITPC), My Channels (MYCHANNELS)
0 Kudos
RokuMarkn
Visitor

Re: roAssociativeArray.append() with duplicate keys

"squirreltown" wrote:

Brightscript is case-insensitive except for the registry, right? Anything else?


From the Brightscript Language Reference:


The dot operator is always case insensitive. By convention, a statement like:

aa.NewKey = 55

Will actually create the Associative Array entry in all lower case ("newkey"). The array operator or the ifAssociativeArray.AddReplace method can be used to create mixed case keys.


--Mark
0 Kudos
EnTerr
Roku Guru

Re: roAssociativeArray.append() with duplicate keys

"TheEndless" wrote:
Sorry, it seems case sensitivity is on when parsing a JSON string, so this is only true if you call the SetModeCaseSensitive() explicitly, or if your AA comes from a parsed JSON string.

Thank you for the heads-up. I see the documentation mentions explicitly (yes! i am glad) the roAAs returned by ParseJSON will be in case-sensitive mode. It occurred to me a way to circumvent that as issue would be:
BrightScript Debugger> test1 = ParseJSON("{" + Chr(34) + "Test" + Chr(34) + ": true}")
BrightScript Debugger> test2 = {}
BrightScript Debugger> test2.append( ParseJSON("{" + Chr(34) + "test" + Chr(34) + ": false}") )
BrightScript Debugger> ? test2
test: false

BrightScript Debugger> test2.append(test1)
BrightScript Debugger> ? test2
Test: true
It was a guess and i tested it - turns out .append() honors the case-sensitivity mode flag of the host AA, so just pouring from a case-sensitive into an insensitive AA "fixes" our issue. IMO things are in order here.
0 Kudos