lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Mon, Jul 16, 2012 at 10:59 AM,  <meino.cramer@gmx.de> wrote:
> Paul K <paulclinger@yahoo.com> [12-07-16 18:02]:
>> HI Meino,
>>
>> >     for key, value in ipairs{...} do
>> >     -- something meaningful here
>> >     end
>>
>> You should be using pairs instead of ipairs and you probably get
>> exactly what you are looking for: 'key' will get values 'first' and
>> 'second' (with 'value' being the corresponding table value), which you
>> can then handle in your script any way you want.
>>
>> Paul.
>>
>> On Sun, Jul 15, 2012 at 8:40 PM,  <meino.cramer@gmx.de> wrote:
>> > Hi,
>> >
>> > Currently I am programming a piece of code what will become a reader
>> > and handler for config files. The starting point was an example code
>> > from "Programming in Lua". The final code should read a config file
>> > with different profiles to setup and handle serial ports so this  is
>> > only an example.
>> >
>> > My config file has this layout currently:
>> >
>> >     profile{
>> >         first={
>> >             author="Donald E. Knuth",
>> >             title= "Literate Programming",
>> >             press= "CSLI",
>> >             year=  1992
>> >             },
>> >         second={
>> >             author=      "Jon Bentley",
>> >             title=       "More Programming Pearls",
>> >             press=       "Addison-Wesley",
>> >             year=        1990
>> >             }
>> >     }
>> >
>> > where "first" and "second" are names of different profiles, which the
>> > user is allowed to name as s/he wants.
>> >
>> > A sketch of a snippet of code which reads the config file looks like
>> > this:
>> >
>> >
>> >
>> >     #! /usr/bin/lua
>> >     local count = 0
>> >     function profile (...)
>> >
>> >     for key, value in ipairs{...} do
>> >     -- something meaningful here
>> >     end
>> >
>> >     end
>> >
>> >     function main()
>> >     dofile("data")
>> >     end
>> >
>> >     main()
>> >
>> > My question is: Is it possible to determine the names of the profiles
>> > (in my example "first" and "second") from inside the code without
>> > further input from the user land?
>> >
>> > Thank you very much in advance for any help!
>> >
>> > Best regards,
>> > mcc
>> >
>> >
>> >
>> >
>>
>
> Hi,
>
> thanks for the replies! :)
>
> Unfortunately apparently I am still doing something wrong...
>
> This is the contents of the config file. The file is called 'data':
>
>
>     profile{
>         first={
>             author="Donald E. Knuth",
>             title= "Literate Programming",
>             press= "CSLI",
>             year=  1992
>             },
>         second={
>             author=      "Jon Bentley",
>             title=       "More Programming Pearls",
>             press=       "Addison-Wesley",
>             year=        1990
>             }
>     }
>
>
> This is the program, called 'reader.lua':
>
>
>     #! /usr/bin/lua
>
>     function profile (...)
>       for key, value in pairs{...} do
>         print( key )
>         print( value )
>       end
>
>     end
>
>     function main()
>       dofile("data")
>     end
>
>     main()
>
>
> and this is the output it produces:
>
>     1
>     table: 0x23420e0
>
>
> (all listings have been indented by four spaces for readablity
> reasons)
>
> What do I have to change to get the table names ('first",'second')
> instead of the adress of the table 'profile" (as it seems, since it
> is only one adress printed) ?
>
> Thank you very much for your help and your patience with lua newbie ;)
>
> Best regards,
> mcc

The code is working exactly how you've written it. ;) Remember that
the parameter to profile() is itself a table.

Consider this:

function profile (cfg)
    for key, value in pairs(cfg) do
        print( key )
        print( value )
    end
end

/s/ Adam