This is a follow up post to the following two other posts....
2. Map Tutorial (Intermediate)
In this post I am going to talk about some more advanced ideas concepts in regards to using maps and lists together.
If you are reading this you are already aware of the power of using lists and maps together to create powerful DS scripts.
I'm going to cover HOW TO COMBINE A ZC MAP AND A ZC LIST TO CREATE MULTI-DIMENSIONAL ARRAYS( Think form records stored in script format).
this into JSON... A PHP multidimensional associative array looks like this....
$client_array = array(
"client_address"=>array(
"street"=>"PO Box 1000",
"city"=>"Seattle",
"state"=>"WA",
"zip"=>"98122"
),
"contact_info"=>array(
"mobile"=>"555-1212",
"email"=>"john@acme.com"
)
);
In the above PHP example the $client_array would be a ZC Deluge Script list() where list.size() == 2.
the client_address and client_info arrays would be ZC Deluge Script map()'s
Stephen, Why would I want to make a multi-dimensional array in Zoho Creator?
1. Store records in script format to be added to a new application upon loading it for the first time. (instead of XML or a getURL() call)
2. Convert cumbersome XMLList()'s to an easy list/map format...
Replace _.executeXPath("/row/blah/blah/text()"); with a simple map.get("key");
NOTE: Try executing an XPath on an xmlList you called through a function! Your Xpath variable will come back null! This solves that problem/bug. (Let me know if you have solved this issue in the past.)
3. Store huge amounts of structured records data IN a variable to be looped through later.
Recently I have been doing a lot of Zoho Creator API work with PHP & jQuery/Javascript coding using the JSON feed instead of XML... and I LOVE IT!
JSON is such a great data format and it's so easy to traverse over/iterate through the records in JSON..
Example: Traversing over JSON in Javascript
for (i = 0; i < length; i++) {
var name = suppliers[i].name;
var address = suppliers[i].address;
var zoho_id = suppliers[i].zoho_id;
});
Now back to Zoho Creator Maps! Zoho Creator Maps ARE VALID JSON when you output them to string..
But, at first I was completely unable to traverse over ZC Maps like you do in regular scripting (like the above JS example).
If you have noticed there is no "for each" option in the map manipulations on the script builder menu AND there isn't really a clear cut way to store key,value pairs in a regular list. (You could mess with indexes or create text deliminators and stuff but it's sort of a bad way of doing things.)
So I tried putting map() variables inside a list. This way I could store my maps inside a format that Zoho Creator permits looping.
If you try to put a map() variable inside a list this is what you get.......
Error at line : 13
Unsupported type given as argument
BUT! What if you convert your map variable to a STRING! Then your list is just holding text (text that is perfectly willing to be converted back into a map later AND is perfectly willing to be saved in a list!)
Let's look at an example of how we can create a MULTI-DIMENSIONAL ARRAY in Zoho Creator....
2. We created a "start_list" with node1 through node 4.
start_list = {"node1", "node2", "node3", "node4"};
The nodes are simply a list telling us how many iterations we are going to have. This could instead be a collection list of records or rows in an XML list perhaps.
3. Now we create an "end_list" this list will hold our key/value pairs (created by the map function).
end_list = List();
4. We create a new map, PUT our keys and values into the map, then convert it to a string!
OUTPUT FROM HERE!
[{"field4":4,"feild3":3,"field1":1,"field2":2}, {"field4":4,"feild3":3,"field1":1,"field2":2}, {"field4":4,"feild3":3,"field1":1,"field2":2}, {"field4":4,"feild3":3,"field1":1,"field2":2}]
Here we create a "test_list" to grab our "values" from the array. We create a new map variable "map" then convert the map string BACK INTO A MAP. Then we just add the the map value to our list and "debug/Info" the test_list..
SURE ENOUGH THE "test_list" comes back with
1, 1, 1, 1
ISN'T THAT GREAT! If you start to think about it more you will see the similarities to the "for each record" function