Field calculation for loop incremented by 1

10238
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
JeffWard
Occasional Contributor III
Right click on the field and choose "Field Calculator", select the Python radio button at the top and make sure the Show Codeblock box is checked.  Then select and copy lines of code in the first code block below and put them in the Pre-Logic Script Code block.

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
If you copy from the above code block, it should keep the proper indentation.

Then select and copy the below code and paste it into the box below YOURFIELDNAME =

autoIncrement()
That should run and get you the results you need.  If not, you probably are trying to put a number in a text field or something.

I have attached a screen capture of what my field calculator dialog looks like.
Jeff Ward
Summit County, Utah
0 Kudos
JeffWard
Occasional Contributor III
Also make sure that you have 0 records selected or the field calculator will only calculate values for the selected records.  I can't tell you how many times I have put in a script and had it run without any errors but it didn't look like any of the field values were changed.  They were changed, just the values for the selected records.
Jeff Ward
Summit County, Utah
0 Kudos
KimballHansen
New Contributor
That worked perfectly. Thanks Jeff. When I ran it however it listed the numbers in order according to the OBJECTID field. I would like to have it run the calculator when I sort a different field ascending. I want to list my streets in alphabetical order and create this new field and run the script so the numbers ascend according to the aphabetical streets. Do you know if it's possible to do that or does it always sort according to the OBJECTID field?
0 Kudos
TerrySilveus
Occasional Contributor III
That worked perfectly. Thanks Jeff. When I ran it however it listed the numbers in order according to the OBJECTID field. I would like to have it run the calculator when I sort a different field ascending. I want to list my streets in alphabetical order and create this new field and run the script so the numbers ascend according to the aphabetical streets. Do you know if it's possible to do that or does it always sort according to the OBJECTID field?


That's what I suggested would happen in a previous post.  If you give my code a try as a python script you will be able to set any order you want.  I just am not sure how to translate that to the field calculator, because I don't use the field calculator that much.
0 Kudos