Forum Discussion

EnTerr's avatar
EnTerr
Roku Guru
12 years ago

roAssociativeArray.append() with duplicate keys [solved]

Documentation is laconic
Append(aa as Object)
Append an AssociativeArray to this one.

It is unclear what happens if the same keys are present in both dictionaries? E.g. a={k:"a"} and b={k:"b"}, after a.append(b), should that be:
a) error during update
b) a.k = "a" (a prevails)
c) a.k = "b" (b prevails)
d) behavior undefined (can change in the future)

I tested and current behavior seems to be (c), as if a loop over b has been ran, doing a.AddReplace(). Roku*, can you state if this will be the behavior to rely on and document it?

PS. method probably should have been named "update()" since this implies new values win, compare to python's

PPS. there is a bug in ".append()" in that if one passes non-roAssociativeArray argument, nothing happens (not even an error). That does not sound meaningful by design. Currently by the doc it is "Append(aa as Object)" but i can feed it number, string, bool, function... with no apparent effect. So seems to be more of "dynamic" type

16 Replies

  • renojim's avatar
    renojim
    Community Streaming Expert
    "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
  • "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.
  • "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?
  • 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
  • "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
  • "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.