[Asterisk-Users] Loops and Variables

Douglas Garstang dgarstang at oneeighty.com
Mon Feb 20 10:14:55 MST 2006


It was trying to perform looping in the dialplan that made me seriously look at AGI. Gee, I wonder what's easier.

This:
exten => s,1,Set(COUNT=0)
exten => s,2,Goto(loop,1);this is where we start the loop
exten => loop,1,GotoIf($[${COUNT} > 5]?next,1);exit if more than 5 esle start again
exten => loop,2,Set(COUNT=$[${COUNT} + 1])
exten => loop,3,Goto(1)
exten => next,1,Noop(${COUNT})

or this...
loop = 0
while loop < 5:
	do-something
	loop += 1

I really wasn't enthused about having to look at dialplan code months later and try and work out what I did earlier. 
Nasty!


-----Original Message-----
From: Tzafrir Cohen [mailto:tzafrir at cohens.org.il]
Sent: Monday, February 20, 2006 9:14 AM
To: asterisk-users at lists.digium.com
Subject: Re: [Asterisk-Users] Loops and Variables


On Mon, Feb 20, 2006 at 08:56:41AM -0500, C F wrote:
> I usually do the same for IVRs, but I always make sure not to use
> itself as the increment, and I use a tempvar instead, like this:

Why?

> exten => s,1,Set(COUNT=0)
> exten => s,2,Goto(100);this is where we start the loop
> exten => s,100,Set(TCOUNT=${COUNT})
> exten => s,101,Noop(${COUNT})
> exten => s,102,GotoIf($[${COUNT} > 5]?150);exit if more than 5 esle start again
> exten => s,103,Set(COUNT=$[{TCOUNT} + 1])
> exten => s,104,Goto(100)
> exten => s,150,Noop(${COUNT})

Why not simply:

> exten => s,1,Set(COUNT=0)
> exten => s,2,Goto(100);this is where we start the loop
> exten => s,100,GotoIf($[${COUNT} > 5]?150);exit if more than 5 esle start again
> exten => s,101,Set(COUNT=$[${COUNT} + 1])
> exten => s,102,Goto(100)
> exten => s,150,Noop(${COUNT})

Alternatively:

> exten => s,1,Set(COUNT=0)
> exten => s,2,Goto(loop,1);this is where we start the loop
> exten => loop,1,GotoIf($[${COUNT} > 5]?next,1);exit if more than 5 esle start again
> exten => loop,2,Set(COUNT=$[${COUNT} + 1])
> exten => loop,3,Goto(1)
> exten => next,1,Noop(${COUNT})

-- 
Tzafrir Cohen         | tzafrir at jbr.cohens.org.il | VIM is
http://tzafrir.org.il |                           | a Mutt's  
tzafrir at cohens.org.il |                           |  best
ICQ# 16849755         |                           | friend

_______________________________________________
--Bandwidth and Colocation provided by Easynews.com --

Asterisk-Users mailing list
To UNSUBSCRIBE or update options visit:
   http://lists.digium.com/mailman/listinfo/asterisk-users



More information about the asterisk-users mailing list