Page 1 of 1

Creating multidimensional arrays with the split command

Posted: Mon Jun 06, 2011 4:27 am
by Peter K
I used the split command to create an array. I retrieved the data from a SQL command that returned one row and everything worked out ok.

I then changed the SQL command to return more than one row. I can't figure out how to get the split command to work properly. The data that comes back from the SQL command is as follows:
1, 3.375, 360, 1
2, 3.375, 360, 1
3, 3.875, 360, 1
4, 4.625, 360, 1

I'd like to get the split command to work so I have an array that is 4 by 4. Since I'll be using the information in the array in some math I need to be able to access each item in the array. As an example, the 3.875 should be tList[3,2].

Any ideas?

Thanks,

Peter

Re: Creating multidimensional arrays with the split command

Posted: Mon Jun 06, 2011 6:59 am
by mwieder
I don't think the split command is set up to handle multidimensional arrays. But here's a function that should do the job for you.

Code: Select all

on mouseUp pMouseBtnNo
    local tSQL
    local tDataArray
    
    -- initialize things to fake a return of SQL data
    put "1, 3.375, 360, 1" & cr & \
          "2, 3.375, 360, 1" & cr & \
          "3, 3.875, 360, 1" & cr & \
          "4, 4.625, 360, 1" & cr into tSQL
    put ConvertToArray(tSQL) into tDataArray
    breakpoint
end mouseUp

function ConvertToArray pText
    local tArray
    local tRow, tColumn
    
    set the itemdelimiter to comma
    put 1 into tRow
    repeat for each line tLine in pText
        put 1 into tColumn
        repeat for each item tItem in tLine
            put tItem into tArray[tRow][tColumn]
            add 1 to tColumn
        end repeat
        add 1 to tRow
    end repeat
    return tArray
end ConvertToArray
...and do note that your array syntax is wrong. Or rather it *is* valid syntax but won't get what you want. Instead try

Code: Select all

tList[3][2]
that will give you your 3.875.