3

What I have: I have 4 fields for my address ranges. L_ADD_FROM, R_ADD_FROM, L_ADD_TO & R_ADD_TO (all in DOUBLE field type).

What I need: The lesser value from L_ADD_FROM and R_ADD_FROM to populate in a field called FROMADDRESS.

I've searched extensively, trying Python and VB Script without much luck. What's the best method for this?

enter image description here

  • What is your script you have written so far? – Hornbydd Mar 11 '15 at 21:37
  • in Python use min([!FROM!,!TO!]) on destination field – FelixIP Mar 11 '15 at 22:06
6

The following approach, using the Python parser, selects the min value from a list of input values.

min(!L_ADD_FROM!, !R_ADD_FROM!)

enter image description here

enter image description here

  • Wow, thanks a lot. That worked, there appears to be 800 ways to do this! – SpinTexas Mar 11 '15 at 22:11
  • 1
    you could even get rid of a few lines and just have return min([x,y]) as your entire function definition. Get rid of the vals list altogether. – Fezter Mar 12 '15 at 2:11
  • 2
    That's a really good outside-the-box way of doing it Aaron, +1 from me. As @Fezter indicated that would work if the only line was return min([x,y]) or even return min(x,y)... without the codeblock! (i.e. min(!L_ADD_FROM!, !R_ADD_FROM!) in this case even NULL can be considered - min(None,Value) returns None, if that's not acceptable then there would be a need for the codeblock (if not x == None..). – Michael Stimson Mar 12 '15 at 2:29
  • Good call @Fezter and Michael Miles-Stimson. Post edited to address comments. – Aaron Mar 12 '15 at 11:34
3

Right-click FROMADDRESS, as you want to populate that one, and open the Field Calculator.

  • Check the Python button
  • Check Show Codeblock

Now, in the bigger area you will insert this function:

def findSmallest(l_add, r_add):
    if l_add < r_add:
        return l_add
    else:
        return r_add

And in the smaller part below the "pre-logic script code" you will call that function, like this:

findSmallest(!L_ADD_FROM!, !R_ADD_FROM!)

This will iterate through each row in the table and put the value of R_ADD_FROM into l_add and the value of R_ADD_FROM into r_add, and then it will compare both. If l_add is smaller, it will write that one into FROMADDRESS (i.e. it will return it), and if it is not (i.e. if it is bigger), it will return the other value.

There is not right or wrong. You could surely do this with VB Script, but getting into Python is a smarter move as it is (becoming) the lingua franca of GIS scripting.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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