Herman Posted November 9 Posted November 9 Hello members, since this is new I want to learn something about tables and this EV. Have a look at this table. The number in "value" indicates the order in which the element was inserted into the table. You see that the name is ordered Alphabetic, I did not do that. Hallo Mitglieder, da dies neu ist, möchte ich etwas über Tabellen und diese EV lernen. Schauen Sie sich diese Tabelle an. Die Zahl in „Wert“ gibt die Reihenfolge an, in der das Element in die Tabelle eingefügt wurde. Sie sehen, dass der Name alphabetisch geordnet ist, ich habe das nicht getan. Have a look when going trough all the elements (name + value) Schauen Sie sich alle Elemente (Name + Wert) an, wenn Sie sie durchgehen The question : it seems totaly random, I want just to know is it so ? / Die Frage: es scheint total zufällig zu sein, ich möchte nur wissen, ob es so ist? Kind regards, Herman
Phrontistes Posted November 9 Posted November 9 vor 12 Minuten schrieb Herman: it seems totaly random, I want just to know is it so ? yes it is. "For items in a table" is converted to Lua: for i, Iter in pairs(t) do end The pairs() function allows you to iterate over a table with arbitrary indices. The order in which the values appear is not defined.
Herman Posted November 9 Author Posted November 9 @Phrontistes , I used Lua. The pairs(t) did me now remember that there is also an ipairs(t) where i is numerical. For me the answer is clear. Thank you @Phrontistes Regards, Herman
Phrontistes Posted November 9 Posted November 9 vor 21 Minuten schrieb Herman: I used Lua. It would make sense to publish the code in the future and not just the result. vor 22 Minuten schrieb Herman: ipairs(t) where i is numerical. Yes. However, ipairs() is only of limited use, since it always starts at index 1 (0-based arrays/tables are therefore only possible in a roundabout way) and does not count up to the highest index, but only until it encounters a nonexistent index.
Goetz Posted November 9 Posted November 9 vor 25 Minuten schrieb Herman: there is also an ipairs(t) in ipairs() returns only those fields of a table, which are numbered sequentially and consecutively. In other words: That which looks and behaves like a list. in pairs() returns all fields from a table, but in no particular order. Regardless, how the fields where created and how they are indexed, you cannot predict the order in which the values are returned.
Phrontistes Posted November 9 Posted November 9 (edited) Hallo @Herman vor 3 Stunden schrieb Phrontistes: The order in which the values appear is not defined. You can make a bubblesort to sort the elements alphabetically: function bubbleSortStrings(arr) local n = #arr for i = 1, n do for j = 1, n - i do if arr[j] > arr[j + 1] then arr[j], arr[j + 1] = arr[j + 1], arr[j] end end end end -- Example local array = {"Banana", "Apple", "Cherry", "Blackberry"} bubbleSortStrings(array) print("Sorted array:") for i = 1, #array do print(array[i]) end Kind regards Phrontistes Edited November 9 by Phrontistes typo
Herman Posted November 10 Author Posted November 10 (edited) Hello @Goetz , @Phrontistes , thanks for the explanations. I was a bit distracted because it is only a new graphical EV command. Translated in Lua "for i, Iter in pairs(t) do "; and we used this Lua version in previous versions already. I should have seen that, but sometimes ... To avoid confusion : this is the new V9 command now. For the rest see explanations above from Götz and Phrontistes. Hallo @Goetz, @Phrontistes, danke für die Erklärungen. Ich war ein wenig abgelenkt, weil es sich nur um einen neuen grafischen EV-Befehl handelt. Übersetzt in Lua „for i, Iter in pairs(t) do “; und wir haben diese Lua-Version bereits in früheren Versionen verwendet. Das hätte ich sehen müssen, aber manchmal ... Um Verwirrung zu vermeiden: dies ist jetzt der neue V9 Befehl. Für den Rest siehe die obigen Erklärungen von Götz und Phrontistes. Thanks again for the help, kind regards, Herman @Neo , thanks for moving it to problems and solutions. Edited November 10 by Herman
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now