value to string constant

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

value to string constant

meino.cramer
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





Reply | Threaded
Open this post in threaded view
|

Re: value to string constant

Coda Highland
On Thu, Jul 19, 2012 at 8:39 PM,  <[hidden email]> 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

Reply | Threaded
Open this post in threaded view
|

RE: value to string constant

Thijs Schreijer
In reply to this post by meino.cramer
> -----Original Message-----
> From: [hidden email] [mailto:[hidden email]]
> On Behalf Of [hidden email]
> Sent: vrijdag 20 juli 2012 5:39
> To: Lua mailing list
> Subject: value to string constant
>
> 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
>

The key you're trying to find is a number, while the table has a string as a
key. This is one of those little things that can easily go wrong. Do an
explicit coercion to prevent this.
Try;
     baudrate = { 115200, "rs232.RS232_BAUD_115200" }
     ui=115200

     local c = baudrate(tonumber(ui) or -1)
     print (c or "Baudrate not found")

tonumber(ui) returns nil if it can't convert which will throw an index
error, hence the 'or -1', so you'll only need to check whether 'c == nil'
(or another 'or' construct in the print statement)


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



Reply | Threaded
Open this post in threaded view
|

Re: value to string constant

Daurnimator
In reply to this post by Coda Highland
On 20 July 2012 14:11, Coda Highland <[hidden email]> wrote:
> 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
>

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

Reply | Threaded
Open this post in threaded view
|

Re: value to string constant

Frank Meier-Dörnberg
Am 20.07.2012 07:00, schrieb Daurnimator:
> You're close but you don't have your table constructor right. Try
> baudrate = { [115200] = "rs232.RS232_BAUD_115200" }

... just in case we are not talking about "mapping" but about
"converting without any checks":

ui = 115200
baudrate = "rs232.RS232_BAUD_" .. ui


Reply | Threaded
Open this post in threaded view
|

Re: value to string constant

Coda Highland
In reply to this post by Daurnimator
On Thu, Jul 19, 2012 at 10:00 PM, Daurnimator <[hidden email]> wrote:

> On 20 July 2012 14:11, Coda Highland <[hidden email]> wrote:
>> 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
>>
>
> You're close but you don't have your table constructor right. Try
> baudrate = { [115200] = "rs232.RS232_BAUD_115200" }
>

Argh. XD JSON bites me again. Thanks.

/s/ Adam