[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: value to string constant
- From: Coda Highland <chighland@...>
- Date: Thu, 19 Jul 2012 21:11:38 -0700
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