Forum

> > CS2D > Scripts > Values returned by function in a table?
Forums overviewCS2D overview Scripts overviewLog in to reply

English Values returned by function in a table?

15 replies
To the start Previous 1 Next To the start

old Values returned by function in a table?

Avo
User Off Offline

Quote
I was thinking about return values of functions

1
2
3
4
tbl={}
function _test()
	return 1,2,"Hello World"
end
and I want to have all values returned by function "_test":

1
tbl={1,2,"Hello World"}

It could be done just like:
1
2
3
4
5
tbl={}
function _test()
	return {1,2,"Hello World"}
end	
tbl=_test()

Is it possible in another way?

old Re: Values returned by function in a table?

Avo
User Off Offline

Quote
@user Snurq: Are you not serious?

BTW:
I probably solved it:
1
2
3
4
5
6
7
8
9
function test(r)
	local t={}
	for i=1,r do
		table.insert(t,i)
	end
	return t
end

table.foreach(test(10),print)

I just have to return a table with values in it...

old Re: Values returned by function in a table?

Lee
Moderator Off Offline

Quote
No he's serious, passing a function call to the tail of a list will automatically expand the return values into the table.

1
2
3
4
5
6
7
8
function test()
	return 3,4,5
end

print(unpack({test()})) --  3 4 5
print(unpack({1,2,test()})) -- 1 2 3 4 5
print(unpack({test(), 5,7})) -- 3 5 7
print(unpack({1,test(),5})) -- 1 3 5

This was introduced when the same bytecode primitive for top-of-stack expansion was introduced for vararg and also for returning multiple values.

Consequently, this makes it impossible to determine the size of a "constant" initialized table during parsing.

old Re: Values returned by function in a table?

Avo
User Off Offline

Quote
So does should work with all functions from lua libraries?

1
2
txt="Lua Lua Pacal C++"
tbl={unpack(string.find(txt,"Lua"))}
tbl={1,3} ?
edited 1×, last 06.07.12 09:49:06 am

old Re: Values returned by function in a table?

EngiN33R
Moderator Off Offline

Quote
user omg has written
lua IS primitive lol...doesnt support objects, anyway except for metatables, maybe

The good thing about Lua is its versatility. With advanced features of Lua you can implement classes and objects functionality with relative ease. Don't call it primitive. It wasn't exactly designed by cavemen, nor for cavemen.
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview