2

I'm making a model builder to automatize some linear referencing projects but I have some problems with the field calculator tool.

My table contains 3 fields : ID | NOM | LIGNE and I'd like to complete the "LIGNE" field.

This row is numeric and it is composed by the 6 first number of the ID number 1.

I made a little picture to illustrate my problem :

image_problem
(source: canardpc.com)
image

To resume, I'd like to complete my "LIGNE" field with the code "456789" which is the code line of the first ID.

Please... Could you help me ??

1
  • 2
    Please accept the answer that solved your problem. To mark an answer as accepted, click on the check mark beside the answer to toggle it from hollow to green
    – BERA
    Oct 10 '16 at 17:12
4

Use the field calculator, select your field 'ligne' and try:

Split( [NOM] , " ")(0)

It splits the field (in your case "NOM") by whitespace characters (" ") and returns the first (index 0) segment.
This is a string operation, so the resulting data type is also a string. In case your result field has another type (e.g. integer), you have to convert it, like

CInt(Split( [input_field] , " ")(0))

If the input value can be empty, you have to check first by using VBScript (in Pre-Logic Script Code), e.g.

value =  Split( [input_field] , " ")
If ubound(value) >= 0 Then
  result = value(0)
Else
  result = ""
End if

and assign "result" to your field

2
  • Hello ! Thank you for your help but it dosen't work. The result is good juste for the first ID and the others are "NULL". I have the same result when I made "left([NOM],6)". What I want to do is to input the first value of the "NOM" field to the others.
    – bp03
    Oct 10 '16 at 11:43
  • @bp03: see the updated script, you have to put it in the "Pre-Logic Script Code" field, thanks to @fatih_dur!
    – Greg Z
    Oct 10 '16 at 12:21
4

Instead of Field Calculator you can use a python script and updatecursor. A python script can be can be integrated into models or executed inside ArcMap using the python window.

import arcpy
table = r'C:\TEST.gdb\Table123'
with arcpy.da.UpdateCursor(table,['NOM','LIGNE']) as ucurs:
    for row in ucurs:
        try:
            index=str(row[0]).index(':')
            value=int(row[0][:index])
            row[1]=value
        except ValueError:
            row[1]=value
        ucurs.updateRow(row)

enter image description here

2

Try this (with an evident inspiration from @Greg Z) after setting your parser to Python and selecting Show Codeblock (inside Pre-Logic Script Code):

nom=None

def ligne_process(val):
    global nom
    val=long(val) if val.strip().isdigit() else False        
    if val and nom!=val:            
        nom=val
    return nom

And paste the following code in the smaller box below the Pre-Logic Script Code

ligne_process(!NOM!.split(":")[0])

This code assumes, as @Greg Z highlighted, your target integer ID in NOM field is the first part just before the first colon (:) and your LIGNE field is an integer/numeric type.

EDIT

I have edited my answer as to respond your linear referencing related enquiry.

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.