Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

The operators in this section are valid for maps. Many of these operators take an argument that is a hash path. A hash path is an array whose elements represent the key values (for a map) or array indices of a path from the root of a complex data structure to the element of interest. For example, consider the following data structure:

{ "colors" : "many",
  "pi" : [3,1,4,1,5,6,9],
  "foo" : {"bar" : {10:"I like cheese"}}
}

The hash path ["foo", "bar"] references the following map inside the data structure:

{"bar" : {10:"I like cheese"}}

delete()

The delete() operator takes a hash path argument designating the member of the original data structure that should be deleted. The delete() operator returns a new data structure with the specified member deleted. If the path is invalid, nothing is deleted and the new data structure is identical to the original. For example:

a = { "colors" : "many",
      "pi" : [3,1,4,1,5,6,9],
      "foo" : {"bar" : {10:"I like cheese"}}
    };
c = a.delete(["foo", "bar", 10])
// c = { "colors" : "many",
//       "pi" : [3,1,4,1,5,6,9],
//       "foo" : {}
// }

encode()

The encode() operator returns a string containing the JSON representation of the data structure to which it is applied. For example:

a = {"a" : 10, "b" : [1, 3, "hello"]};
b = a.encode() // b = "{'a' : 10, 'b' : [1, 3, 'hello']}";

keys()

The keys()operator returns the keys of the map to which it is applied. Without an argument, the keys at the top level of the map are returned. The operator also accepts a hash path argument. For example:

a = { "colors" : "many",
      "pi" : [3,1,4,1,5,6,9],
      "foo" : {"bar" : {10: "I like cheese", "a" : 12}}
    };
b = a.keys();               // b = ["colors", "pi", "foo"]
c = a.keys(["foo", "bar"]); // c = [10, "a"]

put()

The put() operator takes two arguments: a hash path and a new element to be inserted. The result is a new data a structure with the element inserted at the location specified by the hash path. If the path specifies a location that does not exist, it will be created. For example:

a = { "colors" : "many",
      "pi" : [3,1,4,1,5,6,9]
    };
b = {"flop" : 12};
c = a.put(["foo"], b);
// c = { "colors" : "many",
//       "pi" : [3,1,4,1,5,6,9],
//       "foo" : {"flop" : 12}
//     };
d = a.put(["foo", "bar"], b);
// d = { "colors" : "many",
//       "pi" : [3,1,4,1,5,6,9],
//       "foo" : {"bar" : {"flop" : 12}}
// };

If the first argument is missing (i.e. there is only one argument) then the path is take to be the root and the item is put at the root level of the map:

e = a.put(b);
// a = {"colors" : "many",
//      "pi" : [3,1,4,1,5,6,9]
//      "flop" : 12
//     };
  • No labels