I have in my columns attribute the following table ( follows the image):
"rank" (which is related to the order of my data);
"SL" (which is a linear measure of my object);
"testada" (the formula):
I wanted a help in the implementation of the formula tested in the field command of the calculator arcgis.
The columns "rank" and "SL" range, containing more or less attributes. That is, My data vary and what I'm showing is just a sample of my data set so was wanting a more dynamic formula. This formula has to read up to the last line, and return the sum of tested according to formula.
-
1What is the initial value if testdata? (first row)fatih_dur– fatih_dur2016年10月11日 22:09:02 +00:00Commented Oct 11, 2016 at 22:09
2 Answers 2
You can use a python "closure" construct or a global var, but I prefer not to use globals.
Expression:
calc_testada( !SL! )
Code block:
def testada(initval=0):
#Remember state using a "closure" pattern
#Note closure vars are read-only in Python 2x
#So we use a mutable var like a list
val = [initval]
def _testada(SL):
val[0] += SL
return val[0]
return _testada
calc_testada = testada()
After setting your parser to Python
and selecting Show Codeblock
(inside Pre-Logic Script Code
):
testada = 0 #Assuming your initial value for this is zero, i.e. testada[i-1] for rank 1
def sum_value (val):
global testada
testada += val
return testada
And paste the following code in the smaller box below the Pre-Logic Script Code
sum_value(!SL!)
-
+1 from me. I said in my answer I prefer not to use globals, but that's just in general as I usually write code for larger scripts/libraries where global vars can lead to problems. In this case a global variable won't cause any issues and makes for simpler code than what I posted.user2856– user28562016年10月13日 03:57:13 +00:00Commented Oct 13, 2016 at 3:57
-
As you said, the use of global(s) is context dependent and for a very simple process like this, the interpreter will deal with only one global (and once) and yield.fatih_dur– fatih_dur2016年10月13日 04:19:04 +00:00Commented Oct 13, 2016 at 4:19
Explore related questions
See similar questions with these tags.