I’ve been staring at my code for a while and I haven’t slept so I’m really tired.
I’m trying to write a function that returns the line number that the cursor position is on.
Example: The cursor is on line 321
function getLineFromCursorPosition()
local lines = string.split(TextBox.Text,'\n')
local totalChars = 0
warn(TextBox.CursorPosition)
for i,v in ipairs(lines) do
print(v)
totalChars+=string.len(v)
if totalChars >= TextBox.CursorPosition then
return i
end
end
return -1
end
I’m going to sleep. I’ll look at this riddle later with a fresh pair of eyes.
If anyone wants to help me out in the meantime, I’d appreciate some input as to what I’m doing wrong.
function getLineFromCursorPosition()
local t = {}-- table to store the indices
local i = 0
while true do
i = string.find(TextBox.Text, "\n", i+1)-- find 'next' newline
if i == nil then break end
table.insert(t, i)
end
for i2,v2 in pairs(t) do
if v2>TextBox.CursorPosition-1 then
return i2
end
end
return #t+1
end