03/24/15, 04:27 PM | #1 |
|
table.sort (parameter is nil)?
What could cause the parameter of a function, used in a table.sort(..., function), to have a nil parameter?
In one of my sorts in TextureIt I just encountered an error "Attempt to index a nil value" and I'm not sure how it is happening. It is random, it isn't tied to any specific search. For example I've just been searching "open" & "close" (without quotes). I just keep reloading the ui and doing the searches, sometimes both searches work, sometimes the second parameter passed to the sort function is nil. I tried saving a reference to the table I'm sorting & examining it with /zgoo whenever the error occurs, but it looks ok to me? Code that calls the sort & sort code:
Warning: Spoiler
Lua Code:
I'm not sure if this part has anything to do with it, but I'll include it anyways. This is where the searchResults table gets populated. I pass in a destination table (searchResults) to save the matches into because of the recursion. So I don't have to copy the returned results each time the recursion happens.
Warning: Spoiler
Lua Code:
Anyone have any ideas why table.sort would pass in a nil parameter to my sort function? |
03/24/15, 04:46 PM | #2 |
For me it looks that your function GetSearchResults() will build up a new table with the values of ALL your textues and paths that match to the searched string.
But you'll build a table "searchResults " with non-digit keys here! Or does your table TEXTUREIT_TEXTURES store the key as digits? It looks like you are using text "name of texture" = texture "path to texture"? The function table.sort will only sort, as far as I know, tables with non-alphanumeric key values. Only digits work properly. I guess you'll have to transfer your searchResults table into another table with digits as key and then sort it by your sortfunction and afterwards transfer the results back to your wished output table. Maybe this helps although I'm not sure if this explains the NIL stuff somehow ^^ |
|
03/24/15, 04:56 PM | #3 | ||
|
But that doesn't matter because searchResults has numeric keys, everything that gets put into the searchResults table is inserted with: Lua Code:
So for example: Lua Code:
EDIT: By the way, just incase I had missed something & something was getting added to the table without a numeric key I did check it. That is why I added the line: Lua Code:
Last edited by circonian : 03/24/15 at 05:04 PM. |
||
03/24/15, 05:29 PM | #4 |
Oh yes, I didn't think it to the end.... table.insert does use numeric index automatically, right.
The only explanation why searchResults[index].text would be nil then is: Because it was added to destTable[index] already with a nil value. But I bet you already checked this |
|
03/24/15, 05:55 PM | #5 | |
|
The error is "Attempting to index a nil value" NOT "Operation < is not supported for string < nil" "Attempting to index a nil value" -- Means that the entire table, tableB itself is nil (not just tableB.text) Lua Code:
|
|
03/24/15, 06:59 PM | #6 |
|
Haven't tested anything, but I think table.sort might get confused by your comparing less-or-equal instead of less.
Sort functions commonly require strict total order, that is if cmp(a, b) then not cmp(b, a). Your comparator, when presented with a={text="foo"} and b={text="foo"}, returns true for both tableSort(a, b) and tableSort(b, a); but it should return false for equivalent items. |
03/24/15, 07:11 PM | #7 | |
|
somestring <= somestringWhere it compared some text value to itself twice. I guess that explains why! If it requires a strict comparison/order it couldn't determine which one came first because both evaluations would return true. I never would have thought of that, thanks! |
|
ESOUI » Developer Discussions » Lua/XML Help » table.sort (parameter is nil)? |
«
Previous Thread
|
Next Thread
»
|
Display Modes |
Linear Mode |
Switch to Hybrid Mode |
Switch to Threaded Mode |
|
|