Table.insert should return index

local t = {}

print(table.insert(t, “Text”))

Results in “1” being printed.

4 Likes

table.insert(t, “Text”)
print(#t)

That’s actually a fairly elegant solution.

Why would it be a bad idea btw? I can’t imagine anyone trying to use the results of the function for anything in existing code :open_mouth:

That makes sense, thanks for the input.

Oh I figure it’s worth noting that by keeping your changes out of the default Lua stuff, it makes it easier to just drop-in a newer version of Lua whenever it updates.

I didn’t even know table.insert had a 3rd parameter O_o

1 Like

It’s kind of weird to explain, but the third argument is the second argument at times?

Now in a way people can understand:
It’s actually table.insert(table,position,value).
If you do table.insert({1,2,3},2,"hi") it would result in {1,"hi",2,3}
table.insert(table,value) is short for table.insert(table,#table+1,value)

how did you not know this already? in my mind you’re one of the very good scripters

1 Like

Not sure if it’s just me but I almost never use table.insert… I prefer doing arr[ #arr + 1 ] = value ; due to the super small “performance” gain in time. I hope I did not make a big mistake with the following code ;p

Using: table.insert( ) | 0.80188727378845
Using: insert( ) (using local)| 0.6523756980896
Using: # + 1 | 0.36532235145569

Code
local int               =   10000 ;

local function Benchmark( func )
    local intStart      =   tick( ) ;
    
    for i = 0, int do
        func( ) ;
    end

    return ( tick( ) - intStart ) ;
end

local function TableInsert( )
    local arr           =   { } ;

    for i = 0, 100 do
        table.insert( arr, 123 ) ;
    end
end

local insert            =   table.insert ;
local function TableInsertL( )
    local arr           =   { } ;

    for i = 0, 100 do
        insert( arr, 123 ) ;
    end
end

local function TableCountThing( )
    local arr           =   { } ;

    for i = 0, 100 do
        arr[ #arr + 1 ] =   123 ;
    end
end

print( 'Using: table.insert( ) |', Benchmark( TableInsert ) ) ;
print( 'Using: insert( ) (using local)|', Benchmark( TableInsertL ) ) ;
print( 'Using: # + 1 |', Benchmark( TableCountThing ) ) ;
1 Like

But using the count thing wouldn’t shift everything (if necessary) like table.insert does. (but yes, for just inserting it at the end, the count thing is faster, just as a numeric for loop is faster than using ipairs and next,table is faster than pairs(table). Unless you’re doing some heavy calculating, you should just use what’s easiest and looks best to you)

1 Like

That’s never happened–it’s still running 5.1 without the patches up to 5.1.5. 5.2 is backwards-incompatible ofc.

It does mean less work when they COUGH SWITCHTOLUAJIT COUGH

5 Likes

Libraries can still be updated though which would be nice. Zeuxcg once mentioned adding things like the bitwise library and other changes are possible. I made a thread about it sometime ago.

Interesting thing many people don’t know
table.remove() returns the removed value.

bitwise library is built into LJ nudge nudge wink wink

1 Like