0

I have made a loop in MapBasic but is it only updating every other row in the table:

enter image description here

My code works from the bottom of the table upwards to generate start and end coordinates for polyline features:

    Dim nrows as integer

Dim USX, USY, UEX, UEY as float

For nrows = TableInfo(WorTab, TAB_INFO_NROWS) to 1 Step - 1 
    Select * from WorTab where rowid = nrows
    Dim ObjTYPE as integer
    ObjType = ObjectInfo(Selection.obj, OBJ_INFO_TYPE)
    If ObjType = 4 then
    nPoints = ObjectInfo(Selection.obj, OBJ_INFO_NPNTS)
    USX = ObjectNodeX(Selection.Obj,1,1) 
    USY = ObjectNodeY(Selection.Obj,1,1)
    UEX = ObjectNodeX(Selection.Obj,1,nPoints)
    UEY = ObjectNodeY(Selection.Obj,1,nPoints)
    Update Selection Set SX = USX, SY = USY, EX = UEX, EY = UEY
    nrows = nrows -1
    Close Table Selection
    Else
    End if

I'd be very grateful if somebody could point out what I'm doing wrong and why nrows -1 is the wrong choice.

2

A For Loop automatically adds to or subtracts from the counter variable.

You are subtracting one from it and then the loop itself subtracts one from the counter. That's why it skips every second records in your table.

If you are sure all your records are polylines and that noe of them have more than one segment, you can do this in a single update statement:

Update Selection 
   Set SX = ObjectNodeX(OBJ, 1, 1)
     , SY = ObjectNodeY(OBJ, 1, 1)
     , EX = ObjectNodeX(OBJ, 1, ObjectInfo(OBJ, OBJ_INFO_NPNTS))
     , EY = ObjectNodeY(OBJ, 1, ObjectInfo(OBJ, OBJ_INFO_NPNTS))

This should also be quicker than doing the loop and querying

  • Thanks Peter, I also noticed a glaring issue. It crashed without nrows -1, as it was gradually building up Query tables (>4000)! So I also added Close Table "Query" + 1 to the end and now it works fine! – Alan Carr Apr 2 '14 at 10:41
  • An ever better solution would be to use this structure: " Select * from WorTab where rowid = nrows Into __SOME__NAME NoSelect". In this way you keep overwriting the one query called __SOME__NAME and the NoSelect keyword means that you don't highlight the sected record – Peter Horsbøll Møller Apr 2 '14 at 12:02

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.