Thursday, 7 September 2017

arcgis desktop - using mod() with IF THEN field calculation


I am trying to use the field calculator to assign a "odd" or "even" value to a field of small integers. I usually do this with by selecting where:


MOD([field],2) = 0

and then just calculating it as "even", then reversing the selection and setting it to "odd"


I was hoping to do something like this in the field calculator:



DIM oddeven
IF mod([Field],2) = 0
oddeven = "even"
else
oddeven = "odd"
end IF

This does not work and I assume there is an alternate use of the mod() in this setting, or perhaps you cannot use an operator in the IF/THEN condition statement.



Answer



If you're interested in a Python implementation, make sure you set your parser to "Python" and enter the below function in the Codeblock area. Then, all you have to do is call the function oddeven with the field name surrounded by "!".



def oddeven(n):
if n%2 ==0:
return "even"
else:
return "odd"

enter image description here


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...