Du bist nicht angemeldet.

Stilllegung des Forums
Das Forum wurde am 05.06.2023 nach über 20 Jahren stillgelegt (weitere Informationen und ein kleiner Rückblick).
Registrierungen, Anmeldungen und Postings sind nicht mehr möglich. Öffentliche Inhalte sind weiterhin zugänglich.
Das Team von spieleprogrammierer.de bedankt sich bei der Community für die vielen schönen Jahre.
Wenn du eine deutschsprachige Spieleentwickler-Community suchst, schau doch mal im Discord und auf ZFX vorbei!

Werbeanzeige

1

05.06.2008, 18:55

[eBook/html] Programming in Lua


(Link)

Programming in Lua vom Lua "Erfinder" Roberto Ierusalimschy ist - nomen est omen - ein Buch, das sich mit der gleichnamigen Skriptsprache und ihrer Implementierung in C beschäftigt (in englisch).
In den ersten drei Vierteln des Buches wird dem Leser die Sprache nahegebracht und daraus resultierende Möglichkeiten wie etwa OOP mit Metatables erläutert. Der abschließende Teil ist an C/C++ Entwickler gerichtet, denen erklärt wird, wie man die Skriptsprache in die eigene Anwendung integriert, Daten mittels Stack austauscht, C Funktionen in Lua verfügbar macht etc.

Die erste Auflage dieses Buches gibt es als kostenloses eBook auf der Lua-Website (kein großer Geheimtipp mehr). Grundlage ist noch Lua 5.0, aber da die nachfolgenden Versionen bis zum heutigen Lua 5.1.3 hauptsächlich nur Bugfixes behoben haben, ist das Buch immer noch uneingeschränkt zu empfehlen. Besonders die späteren Kapitel sind echt Eye-Opener, weil man dann erst versteht, wie mächtig diese Sprache wirklich ist. Wer sich mit Lua beschäftigen will, sollte dieses Buch gelesen haben.

Hier ist der Link zum eBook (html): http://www.lua.org/pil/

Die neueste Auflage und die deutsche Übersetzung sind natürlich auf Amazon und in diversen anderen Buchläden erhältlich.


Leseprobe:
5.1 - Multiple Results

An unconventional, but quite convenient feature of Lua is that functions may return multiple results. Several predefined functions in Lua return multiple values. An example is the string.find function, which locates a pattern in a string. It returns two indices: the index of the character where the pattern match starts and the one where it ends (or nil if it cannot find the pattern). A multiple assignment allows the program to get both results:

Quellcode

1
2
3
    s, e = string.find("hello Lua users", "Lua")
    
    print(s, e)   -->  7      9


Functions written in Lua also can return multiple results, by listing them all after the return keyword. For instance, a function to find the maximum element in an array can return both the maximum value and its location:

Quellcode

1
2
3
4
5
6
7
8
9
10
11
12
13
    function maximum (a)
      local mi = 1          -- maximum index
      local m = a[mi]       -- maximum value
      for i,val in ipairs(a) do
        if val > m then
          mi = i
          m = val
        end
      end
      return m, mi
    end
    
    print(maximum({8,10,23,12,5}))     --> 23   3


Lua always adjusts the number of results from a function to the circumstances of the call. When we call a function as a statement, Lua discards all of its results. When we use a call as an expression, Lua keeps only the first result. We get all results only when the call is the last (or the only) expression in a list of expressions. These lists appear in four constructions in Lua: multiple assignment, arguments to function calls, table constructors, and return statements. To illustrate all these uses, we will assume the following definitions for the next examples:

Quellcode

1
2
3
    function foo0 () end                  -- returns no results
    function foo1 () return 'a' end       -- returns 1 result
    function foo2 () return 'a','b' end   -- returns 2 results


[...]

Anonymous

unregistriert

2

08.06.2008, 15:08

Dazu noch was Einsteiger freundliches auf deutsch:

http://lua.gts-stolberg.de/index.php

cu