Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Math Script Will Print 1-10 if numbers are 1-10 without being 2+ than last # wont work?

Asked by
LuaDLL 253 Moderation Voter
6 years ago
Edited 6 years ago
local Table = {1,2,3,4,"FDS",5,6,7,8,9,10}

local Result = nil

local FirstNums = {}
local LastNums = {}
local OtherNum = {}
for i,v in pairs(Table) do
    if tonumber(v) then
        if table.getn(LastNums) == 0 then
            table.insert(LastNums,v)
            table.insert(FirstNums,v)
        else
            if (FirstNums[table.getn(FirstNums)] + 1) == v then
                table.insert(LastNums,v)
            else
                table.insert(OtherNum,v)
            end
        end
    else
        print("NOT A NUMBER")
    end
end

for i = 1,table.getn(FirstNums) do
    if (FirstNums[i] + 1) == LastNums[i] then
        print(FirstNums[i].."-"..LastNums[i])
    else
        for _,v in pairs(OtherNum) do
            print(v)
        end
    end
end

doesn't print any errors

Output: NOT A NUMBER 3 4 5 6 7 8 9 10

0
maybe your if statements are wrapped in () when they are unnecessary! User#19524 175 — 6y
0
No, it worked before with the () around the FirstNums + 1, but i dont know why it isnt working anymore LuaDLL 253 — 6y

1 answer

Log in to vote
0
Answered by 6 years ago
Edited 6 years ago

Not sure what your goal is but here's the script with my comments. As you can see, table.getn(FirstNums) is always 1.

local Table = {1,2,3,4,"FDS",5,6,7,8,9,10}

local Result = nil

local FirstNums = {}
local LastNums = {}
local OtherNum = {}
for i,v in pairs(Table) do
    if tonumber(v) then
        if table.getn(LastNums) == 0 then
            print("-Storing: ",v)
            table.insert(LastNums,v)
            table.insert(FirstNums,v)
        else
            print("-FirstNum is always one: ",FirstNums[table.getn(FirstNums)])
            -- if 1 + 1 == v then
            if (FirstNums[table.getn(FirstNums)] + 1) == v then
                print("-Storing LastNum: ",v)
                table.insert(LastNums,v) -- Will always store 2
            else
                print("-Storing OtherNum: ",v)
                table.insert(OtherNum,v)
            end
        end
    else
        print("NOT A NUMBER")
    end
end

-- For loop iterates once
for i = 1,table.getn(FirstNums) do
    print("-First: ",FirstNums[i] + 1," : Last: ",LastNums[i])
    -- If 1 + 1 == 1 then
    if (FirstNums[i] + 1) == LastNums[i] then
        print(FirstNums[i].."-"..LastNums[i])
    else
        print("-OtherNum")
        for _,v in pairs(OtherNum) do
            print(v)
        end
    end
end

Did you mean to do this?

for i = 1,table.getn(LastNums) do
    if (FirstNums[1] + 1 == LastNums[i]) then
        print(FirstNums[1].."-"..LastNums[i])
    else
        print("-OtherNum")
        for _,v in pairs(OtherNum) do
            print(v)
        end
    end
end
0
That still prints NOT A NUMBER 3 4 5 6 7 8 9 10 1-2 LuaDLL 253 — 6y
0
Let me know what it's supposed to print. It's hard to understand what you're saying. MooMooThalahlah 421 — 6y
Ad

Answer this question