Script Example: Recursively Collect Values from Custom Attributes

Revision as of 09:51, 6 November 2015 by Victor (talk | contribs)

This script recursively collects values of custom attribute contacts from all node parents. Collected values concatenated into single string and separated by semicolons. Duplicate values added only once.

global contacts = "";  // concatenated values will be stored here
global presence = %{ };  // value presence indicator (hash map)

// walk through each parent object for current node
foreach(o : GetObjectParents($node))
{
	add_contacts(o);
}

// Concatenated result is in "contacts" global variable
println "Contacts: " . contacts;

/**
 * Recursively add contacts from object and it's parents
 */
sub add_contacts(curr)
{
	c = GetCustomAttribute(curr, "contacts");
	if ((c != null) && (presence[c] == null))
	{
		if (length(contacts) > 0)
			contacts = contacts . ";" . c;
		else
			contacts = c;
		presence[c] = true;
	}
	
	foreach(o : GetObjectParents(curr))
	{
		add_contacts(o);
	}
}