Forum

> > CS2D > Scripts > Values returned by function in a table?
ForenübersichtCS2D-Übersicht Scripts-ÜbersichtEinloggen, um zu antworten

Englisch Values returned by function in a table?

15 Antworten
Zum Anfang Vorherige 1 Nächste Zum Anfang

alt Values returned by function in a table?

Avo
User Off Offline

Zitieren
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?

alt Re: Values returned by function in a table?

Avo
User Off Offline

Zitieren
@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...

alt Re: Values returned by function in a table?

Lee
Moderator Off Offline

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

alt Re: Values returned by function in a table?

Avo
User Off Offline

Zitieren
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} ?
1× editiert, zuletzt 06.07.12 09:49:06

alt Re: Values returned by function in a table?

EngiN33R
Moderator Off Offline

Zitieren
user omg hat geschrieben
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.
Zum Anfang Vorherige 1 Nächste Zum Anfang
Einloggen, um zu antworten Scripts-ÜbersichtCS2D-ÜbersichtForenübersicht