Field calculation for loop incremented by 1

9945
13
10-04-2011 07:49 AM
KimballHansen
New Contributor
Hi,


I have my attribute table sorted ascending according to my street names (Aprox. 1914 records). I need to make a new field that starts at 10,000 and increments by 1 until it is filled. Is there a way I can do this? I'm thinking of doing some loop that increments by one (because the default for loop in the field calculator repeats the same number) but I don't know how to do that. Any help? Thanks.

Kimball Hansen
Tags (2)
0 Kudos
13 Replies
TerrySilveus
Occasional Contributor III
Hi,


I have my attribute table sorted ascending according to my street names (Aprox. 1914 records). I need to make a new field that starts at 10,000 and increments by 1 until it is filled. Is there a way I can do this? I'm thinking of doing some loop that increments by one (because the default for loop in the field calculator repeats the same number) but I don't know how to do that. Any help? Thanks.

Kimball Hansen


Not sure how you would do it in the field calculator, but you could create a script
    updateTheRows = arcpy.UpdateCursor("C:\\Filename.shp","","","","AddressName A" ) #get the rows from the shapefile order by address ascending
    i = 0    
    for updateTheRow in updateTheRows:
        updateTheRow.setValue("FieldNameToUpdate",10000 + i)
        updateTheRows.updateRow(updateTheRow)
        i= i+1

    del updateTheRow
    del updateTheRows
0 Kudos
KimballHansen
New Contributor
Thanks. I'm very new to python and scripts and don't understand how to begin with that code. Could you please give me a step by step example? Also, which variables are yours that I'm going to cut so I can paste mine?

Kimball Hansen
0 Kudos
TomMagdaleno
Occasional Contributor III
Hi Kimball I know how you feel about being new to python.
Go into your table, right click on the field you want to populate and go to field calculator.  Click the Python button on the top (default is VB).  Paste the code into the field calculator.  TPalmer already changed pStart to 10000 for you, but you can change that number to whatever starting number you need. 

rec=0
def autoIncrement():
global rec
pStart = 10000 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
  rec = pStart
else:
  rec = rec + pInterval
return rec
__esri_field_calculator_splitter__
autoIncrement().
0 Kudos
KimballHansen
New Contributor
I copied that code and when I run the Field Calculator I get an error. The error number is 000539 if that has any relevance to you. Does the field have to be a certain type? (I tried int and double and both didn't work). Or do all the records need to have zero or 1 first in them to us the code? I don't have any other guesses of what to do.
0 Kudos
TerrySilveus
Occasional Contributor III
I copied that code and when I run the Field Calculator I get an error. The error number is 000539 if that has any relevance to you. Does the field have to be a certain type? (I tried int and double and both didn't work). Or do all the records need to have zero or 1 first in them to us the code? I don't have any other guesses of what to do.


There should have been a text message after the number, the text will be more useful to determine the issue, however make sure your indenting is correct for python and that Python is checked as the code type.  Python REQUIRES block indenting

However, I don't believe this code takes into account that you wanted the records numbered in address order.
0 Kudos
KimballHansen
New Contributor
There should have been a text message after the number, the text will be more useful to determine the issue, however make sure your indenting is correct for python and that Python is checked as the code type.  Python REQUIRES block indenting

However, I don't believe this code takes into account that you wanted the records numbered in address order.


I don't know Python so I don't know what parts to indent.
0 Kudos
TerrySilveus
Occasional Contributor III
I don't know Python so I don't know what parts to indent.


rec=0
def autoIncrement():
   global rec
   pStart = 10000 #adjust start value, if req'd
   pInterval = 1 #adjust interval value, if req'd
   if (rec == 0):
      rec = pStart
   else:
      rec = rec + pInterval
   return rec


You should check that you have "use code block" checked and "python" selected
then autoIncrement() goes in the bottom section and the function above goes in the top section "the code block"
0 Kudos
by Anonymous User
Not applicable

I know this is an old thread, but how does it know to loop through the rows? I've seen this bit of code posted elsewhere and I never understood why just a for loop wouldn't work.

0 Kudos
DanPatterson_Retired
MVP Emeritus

In short....that is what the field calculator does... it provides the framework to loop through the rows and apply a function, which is also why it works on selections leaving the unselected untouched.  This would require a separate bit of code in itself to work with selections.  It does have its drawbacks,  like having to use globals to keep track of what has gone on in the previous row (ie a previous value). 

Sometimes the field calculator is just faster for simple things.  It only gets difficult when people try to push the envelop and get it to do things for which it wasn't designed.