Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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. Any ideas would be appreciated! Thanks.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

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

share|improve this answer

Your logic is correct, but the syntax is a bit off. In VBA the mod operator is [Field] Mod 2 and you must use the Then clause at the end of the IF statement. So your corrected code is:

DIM oddeven
IF [TILESQMILE] mod 2 = 0 Then
oddeven = "even"
else
oddeven = "odd"
end IF

And you should input the code into the field calculator window as follows:

enter image description here

share|improve this answer
    
Fantastic! Thank you for the speedy and well explained response. That code did the trick! Thank you. –  Mr.ecos Sep 27 '12 at 20:29
    
You're welcome. Happy to help. –  sgrieve Sep 27 '12 at 20:30

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.