Re: "dumping" a map
- Posted by cjnwl in February
- 1018 views
With some global variables and a couple of wrapper functions :
-- map investigation include std/io.e include std/map.e integer indent = 4 -- spaces per sub-level sequence m_ = "m_" -- submap indicator sequence prefix = ">> " -- prefix before submap name sequence postfix = " >" -- postfix after submap name map tables = new() -- map to hold tables data map kids = new() -- map to hold kids map smap = new() -- map to hold submap procedure map_dump(map m, integer offset) integer s = map:size(m) sequence k = map:keys(m) sequence v = map:values(m) for i = 1 to s do if sequence(k[i]) and equal(m_, k[i][1..2]) then writefln("[][][][]", {repeat(32, offset), prefix, k[i], postfix}) map_dump(v[i], offset+indent) else writefln("[][] = []", {repeat(32, offset), k[i], v[i]}) end if end for return end procedure procedure my_nested_put(map m, sequence s, object v) integer l = length(s) sequence temp = s for i = 1 to l-1 do temp[i] = m_ & temp[i] end for map:nested_put(m, temp, v) return end procedure function my_nested_get(map m, sequence s) integer l = length(s) sequence temp = s for i = 1 to l-1 do temp[i] = m_ & temp[i] end for return map:nested_get(m, temp) end function map:put(tables, "id", 1) map:put(tables, "name", "aaa") map:put(kids, 1, "alice") map:put(kids, 2, "tom") map:put(kids, 3, "wendy") map:put(tables, "m_kids", kids) map:put(smap, "test", "test") map:put(smap, "description", "long description") map:put(kids, "m_test", smap) map:put(tables, "m_test_2", smap) map_dump(tables, 0) --map:nested_put(tables, {"m_kids", "m_test", "value"}, 125) --writefln("\n" & map:nested_get(tables, {"m_kids", "m_test", "description"}) & "\n") --map_dump(tables, 0) writefln("\n============\n") object users = map:new() map:nested_put( users, {"m_alice","id"}, "alice" ) map:nested_put( users, {"m_alice","name"}, "Alice" ) map:nested_put( users, {"m_alice","email"}, "alice@example.com" ) map:nested_put( users, {"m_bob","id"}, "bob" ) map:nested_put( users, {"m_bob","name"}, "Bob" ) map:nested_put( users, {"m_bob","email"}, "bob@example.com" ) map:nested_put( users, {"m_carol","id"}, "carol" ) map:nested_put( users, {"m_carol","name"}, "Carol" ) map:nested_put( users, {"m_carol","email"}, "carol@example.com" ) my_nested_put(users, {"carol", "age"}, 25) map_dump(users, 0) writefln("\n" & my_nested_get(users, {"carol", "email"}))