Jump to content

Recommended Posts

Posted

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.

V9tableiteration3.jpg.4d515069a994bcac25e0b65a4e97a4ec.jpg

Have a look when going trough all the elements (name + value)
Schauen Sie sich alle Elemente (Name + Wert) an, wenn Sie sie durchgehen

V9tableiteration1.jpg.57a90ad5541ff5c60d6ae3c9b5ef6aee.jpg

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

Posted
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.

Posted
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.

Posted
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.

Posted (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 by Phrontistes
typo
Posted (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 ... :x
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 ... :x
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.

 

V9tableiteration4.jpg.f8f1746b41495de0bcf8bdc7916491bd.jpg

 

Thanks again for the help, kind regards, Herman

@Neo , thanks for moving it to problems and solutions. 

Edited by Herman

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...