Select to view content in your preferred language

Counter for While iteration in the model builder

2317
3
11-07-2016 06:04 AM
AlessandroSinglitico
Deactivated User

Hi everyone,

I'm using ArcGis 10.3. I am on the model builder and I have a While iteration. What I want to do is to create a counter: a variable x which is initialized with the value 1 and add +1 at every run, so the variable x actually shows the number of run.

This is because I want to use that value for saving data with in-line substitution (...%x%...).

You can see attached my first attempt. Basically  my Continue is a precondition for Calculate value (2) and the code in it is:

Expression: x("%Continue%")

def x(n):
 if n== "true":
  x=x+1

but I got this error:

ERROR 000539: Error running expression: counter("true")
Traceback (most recent call last):
  File "<expression>", line 1, in <module>
  File "<string>", line 3, in counter
UnboundLocalError: local variable 'counter' referenced before assignment

Is there a nicest and easiest way to create a counter?

(Basically the For iteration has it automatically as the Value, but the While lacked of a variable which takes account of the run)

Alessandro

0 Kudos
3 Replies
DanPatterson_Retired
MVP Emeritus

The for syntax is here For—Help | ArcGIS for Desktop 

and while is here While—Help | ArcGIS for Desktop 

and there are other iterators within the list.  The syntax for why requires a set of inputs and whether to loop until the boolean is either True or False, depending on your choice

0 Kudos
DuncanHornby
MVP Notable Contributor

You say that Calculate value (2)  has the expression:

Expression: x("%Continue%")

def x(n):
 if n== "true":
  x=x+1

But the error message is about counter("true")? You need to show us the whole model or upload it as a zip file as what you are asking is not clear.

Also look at the help file for the subject Examples of in-line variable substitution with ModelBuilder system variables there are two system variables %i% and %n% accessible to you in model builder, may be they are what you are looking for?

AlessandroSinglitico
Deactivated User

Hi Duncan,

Yes I was actually trying to build a counter to use for in-line substitution. It sounds like  the %n% system variable does all the job, there is no need to build anything.

Thank you!

0 Kudos