Quantcast
Channel: Forums - ArcGIS for Desktop - General
Viewing all articles
Browse latest Browse all 2565

In-line if statements in the field calculator, v10.0

$
0
0
The help files don't make it immediately obvious that you can do 'inline' or 'in-line' statements in the field calculator. That is, statements that perform logic based on other attribute fields, but without the incredible tedium of having to create a code block. Field calculator codeblocks in Python are simply the most cumbersome, time-consuming and difficult to debug idea ever, so it's nice to do something on a single line without having to pass all the variables you might need into the function, eg:

Code:

"Bigger" if !var1! > !var2! else "Smaller"
But I want my code to return "Bigger" if var1 > var2, "Smaller" if var1 < var2, and "Same" if var1 = var2

How do I implement this third condition on an inline statement? Can we use else or elif?

My only solution was to execute the first line, then re-run the field calculator with the following:

Code:

"Same" if !var1! == !var2! else !NewField!
Is there a better solution? Anything that can avoid me having to create a codeblock would be very welcome. Thanks

Viewing all articles
Browse latest Browse all 2565

Trending Articles