Hi e90712,
thanks for sharing very nice code, I, as a matrix lover, appreciate it a lot
Just one thing - ThinBasic allows programmer to determine dimensions of array using UBOUND keyword.
So it is possible to avoid run time error 20, you can handle incorrect input on your own.
See modified function:
[code=thinbasic]
FUNCTION digitarrayshow( data_array( ) AS INTEGER, maxdim1 AS INTEGER, maxdim2 AS INTEGER, breakcount AS INTEGER ) as long
LOCAL idim1 AS INTEGER
LOCAL idim2 AS INTEGER
' -- Error checking
select case maxdim1
case < 1
write NL & "[!] MaxDim1 must be positive" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
case > ubound(data_array, 1)
write NL & "[!] MaxDim1 bigger than array can index ("+STR$(ubound(data_array, 1))+" )" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end select
select case maxdim2
case <> 0
if ubound(data_array, 2) = 0 then
write NL & "[!] MaxDim2 specified for one dimensional array" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end if
case < 1
if ubound(data_array, 2) <> 0 then
write NL & "[!] MaxDim2 must be positive" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end if
case > ubound(data_array, 2)
write NL & "[!] MaxDim2 bigger than array can index ("+STR$(ubound(data_array, 2))+" )" & NL
return %FALSE ' -- Not success, RETURN is the same as FUNCTION = ... : EXIT FUNCTION
end select
' -- Functionality
FOR idim1 = 1 TO maxdim1
IF maxdim2 > 0 THEN
FOR idim2 = 1 TO maxdim2
IF MOD ( idim2, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1, idim2 ), " "
NEXT
ELSE
IF MOD ( idim1, breakcount ) = 1 OR breakcount < 2 THEN
write NL
ENDIF
write data_array( idim1 ), " "
ENDIF
NEXT
write NL
function = %TRUE ' -- Success
END FUNCTION
[/code]
UPDATED
Bookmarks