lua-users home
lua-l archive

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


On Thu, Jul 19, 2012 at 8:39 PM,  <meino.cramer@gmx.de> wrote:
> Hi,
>
> from the user input I get the wanted baudrate as an raw value
> - say
>
>     115200.
>
> I have to map this to a constant (in case of luars) which is
>
>     "rs232.RS232_BAUD_115200"
>
> which is a string despite the "." in the middle...
>
> To prevent looping over all constants and forests of if-then-else
> stuff (there are more constants to map) I thought, that a table
> could do that...
>
> But
>
>     baudrate = { "115200", "rs232.RS232_BAUD_115200" }
>
> and
>
>     ui=115200
>
> gives me another problem:
>
>     baudrate.[ui]
>     baudrate.[tostring(ui)]
>     baudrate.ui
>
> produces all 'nil' wiith lua -i
>
> How can I map teh user input directly to the constant without
> iterating and/or 'if-ing' over dozens of possibilities?
>
> Thank you very much in advance for your help and patience with
> a lua newbie... ! :)
>
> Best regards,
> mcc

You're close but you don't have your table constructor right. Try
baudrate = { 115200 = "rs232.RS232_BAUD_115200" }

Then baudrate[115200] will return the appropriate string.

/s/ Adam