Index: funcs/func_enum.c
===================================================================
--- funcs/func_enum.c	(revision 56371)
+++ funcs/func_enum.c	(working copy)
@@ -128,6 +128,8 @@
 		"ENUMLOOKUP(number[|Method-type[|options[|record#[|zone-suffix]]]])",
 	.desc =
 		"Option 'c' returns an integer count of the number of NAPTRs of a certain RR type.\n"
+		"Option '*%d*' (e.g. result%d) returns an integer count of the matched NAPTRs and sets\n"
+		"the results in variables (e.g. result1, result2, ...result<n>)\n"
 		"Combination of 'c' and Method-type of 'ALL' will return a count of all NAPTRs for the record.\n"
 		"Defaults are: Method-type=sip, no options, record=1, zone-suffix=e164.arpa\n\n"
 		"For more information, see doc/enum.txt",
Index: main/enum.c
===================================================================
--- main/enum.c	(revision 56371)
+++ main/enum.c	(working copy)
@@ -61,6 +61,7 @@
 #include <unistd.h>
 #include <errno.h>
 
+#include "asterisk/pbx.h"
 #include "asterisk/logger.h"
 #include "asterisk/options.h"
 #include "asterisk/enum.h"
@@ -295,8 +296,10 @@
 	return 1;
 }
 
-/* do not return requested value, just count RRs and return thei number in dst */
+/* do not return requested value, just count RRs and return the number in dst */
 #define ENUMLOOKUP_OPTIONS_COUNT       1
+/* use options field as a variable name pattern to set (e.g. name%d) */
+#define ENUMLOOKUP_OPTIONS_ARRAY       2
 
 struct enum_naptr_rr {
 	struct naptr naptr; /* order and preference of RR */
@@ -364,27 +367,17 @@
 		ast_log(LOG_WARNING, "Failed to parse naptr :(\n");
 		return -1;
 	} else if (res > 0 && !ast_strlen_zero(c->dst)){ /* ok, we got needed NAPTR */
-		if (c->options & ENUMLOOKUP_OPTIONS_COUNT){ /* counting RRs */
-			c->position++;
-			snprintf(c->dst, c->dstlen, "%d", c->position);
-		} else  {
-			if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
-				c->naptr_rrs = p;
-				memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
-				c->naptr_rrs[c->naptr_rrs_count].result = strdup(c->dst);
-				c->naptr_rrs[c->naptr_rrs_count].tech = strdup(c->tech);
-				c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
-				c->naptr_rrs_count++;
-			}
-			c->dst[0] = 0;
+		if ((p = ast_realloc(c->naptr_rrs, sizeof(*c->naptr_rrs) * (c->naptr_rrs_count + 1)))) {
+			c->naptr_rrs = p;
+			memcpy(&c->naptr_rrs[c->naptr_rrs_count].naptr, answer, sizeof(c->naptr_rrs->naptr));
+			c->naptr_rrs[c->naptr_rrs_count].result = strdup(c->dst);
+			c->naptr_rrs[c->naptr_rrs_count].tech = strdup(c->tech);
+			c->naptr_rrs[c->naptr_rrs_count].sort_pos = c->naptr_rrs_count;
+			c->naptr_rrs_count++;
 		}
 		return 0;
 	}
 
-	if (c->options & ENUMLOOKUP_OPTIONS_COUNT) 	{ /* counting RRs */
-		snprintf(c->dst, c->dstlen, "%d", c->position);
-	}
-
 	return 0;
 }
 
@@ -419,9 +412,17 @@
 	context.naptr_rrs_count = 0;
 
 	if (options != NULL) {
-		if (*options == 'c') {
-			context.options = ENUMLOOKUP_OPTIONS_COUNT;
+		p1 = strchr(options, '%');
+		if (p1 && p1[1] == 'd' && !strchr(p1+1, '%')) {
+			context.options = ENUMLOOKUP_OPTIONS_ARRAY|ENUMLOOKUP_OPTIONS_COUNT;
 			context.position = 0;
+		} else {
+			for (p1 = options; *p1; p1++) {
+				if (*p1 == 'c') {
+					context.options = ENUMLOOKUP_OPTIONS_COUNT;
+					context.position = 0;
+				}
+			}
 		}
 	}
 
@@ -502,7 +503,7 @@
 		ret = 0;
 	}
 
-	if (context.naptr_rrs_count >= context.position && ! (context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
+	if (context.naptr_rrs_count >= context.position || (context.options & ENUMLOOKUP_OPTIONS_ARRAY)) {
 		/* sort array by NAPTR order/preference */
 		for (k = 0; k < context.naptr_rrs_count; k++) {
 			for (i = 0; i < context.naptr_rrs_count; i++) {
@@ -528,16 +529,28 @@
 				}
 			}
 		}
-		for (k = 0; k < context.naptr_rrs_count; k++) {
-			if (context.naptr_rrs[k].sort_pos == context.position-1) {
-				ast_copy_string(context.dst, context.naptr_rrs[k].result, dstlen);
-				ast_copy_string(context.tech, context.naptr_rrs[k].tech, techlen);
-				break;
+		if ((context.options & ENUMLOOKUP_OPTIONS_ARRAY)) {
+			for (k = 0; k < context.naptr_rrs_count; k++) {
+				if (snprintf(dst, dstlen, options, context.naptr_rrs[k].sort_pos+1) >= dstlen) {
+					ast_log(LOG_WARNING, "ENUM buffer too small setting result vars!");
+					break;
+				} else {
+					pbx_builtin_setvar_helper(chan, dst, context.naptr_rrs[k].result);	
+				}
 			}
+		} else {
+			for (k = 0; k < context.naptr_rrs_count; k++) {
+				if (context.naptr_rrs[k].sort_pos == context.position-1) {
+					ast_copy_string(dst, context.naptr_rrs[k].result, dstlen);
+					ast_copy_string(tech, context.naptr_rrs[k].tech, techlen);
+					break;
+				}
+			}
 		}
-	} else if (!(context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
-		context.dst[0] = 0;
 	}
+	if ((context.options & ENUMLOOKUP_OPTIONS_COUNT)) {
+		snprintf(dst, dstlen, "%d", context.naptr_rrs_count);
+	}
 	if (chan)
 		ret |= ast_autoservice_stop(chan);
 
