Sophia Update a better way to write nested arrays

I want to change a nested array. The current way of writing it can update the data and get the result I want, but it feels cumbersome to write it. If I change the nesting layer to three layers, it will be more cumbersome, so is there a better way to write it? I can learn @hanssv.chain

@compiler >= 6

include “String.aes”
include “List.aes”

contract ListInstallTest =

record state = {
    data : list(list(string))}

stateful entrypoint init() = 
    { data = 
        [
            ["1-1","1-2","1-3"],
            ["2-1","2-2","2-3"],
            ["3-1","3-2","3-3"]
        ]}

stateful entrypoint update_data() = 
    let data_list_list = List.get(1,state.data)
    let data_list_list_update =  List.replace_at(1, "update", data_list_list) 
    let data_replace = List.replace_at(1,data_list_list_update, state.data)
    put( { data = data_replace})

entrypoint get_state() =
    state

It all comes down to what data you are storing (remember Sophia/FATE is for smart contracts so large amounts of data is maybe not a great use case) - and perhaps even more how you are accessing and/or updating that data.

For storing: if you have a fixed small size of something you could consider using a tuple for that level of nesting, if data is naturally indexed you could use a map.

For access: if you most of the time access all elements (like looping through them doing something for each element) storing in a list makes sense. On the other hand if you update or access a single element, a map is probably better.

As an example, when solving Advent of Code puzzles in Sophia (back in 2019) I often used a map indexed by a pair of integers as a two-dimensional array - or as a sparse matrix (non-set indexes =0).

1 Like

Thank you ,Any examples? Let me refer to that :grinning:

For example https://github.com/hanssv/AoC2019/blob/master/15/sol_15.aes does some of this - but otherwise it depends a lot on the particular data you are working with.