Comparing data structures


#1

In squirrel, is there an easy way to compare data structures?

An example is:

local data1 = {
    "foo" : "bar",
    "timestamps" : [1436983175, 1436984975, 1436986775, 1436988575, 1436990375],
    "readings" : [32.5, 33.6, 32.8, 32.9, 32.5],
    "otherData" : {
        "state" : true,
        "test" : "test"
    }
}

local data2 = {
    "foo" : "bar",
    "timestamps" : [1436983175, 1436984975, 1436986775, 1436988575, 1436990375],
    "readings" : [32.5, 33.6, 32.8, 32.9, 32.5],
    "otherData" : {
        "state" : true,
        "test" : "test"
    }
}

if(data1==data2) server.log("A match");  // would be too easy

I have tried serializer, json encoding and also converting to at string, but it doesn’t work. The data structure elements are not necessarily stored/converted in the same order.


#2

Maybe no easy way - is there a smart way to do it? :slightly_smiling_face:

I am trying to minimize hits on userconfig memory, when storing settings before periodic deep sleeps …


#3

Take a look at https://github.com/electricimp/impUnit/blob/develop/dist/impUnit.nut#L195-L240 for some inspiration on how to do this recursively. You’ll need to modify slightly to remove the throws and return a true/false, but this will get you close.


#4

Thank you, that is what I am looking for :+1: