Page 1 of 1

Inspecting nested tables

Posted: Wed Apr 05, 2017 8:53 pm
by GMKai
Hello, I try to implement Enums with Hollywood,
this is an excerpt:

Code: Select all

EnumBox = {storage = {}}
EnumEntry = {key ="",msgid="",value=""}

Function EnumEntry:Create(key,msgid,value)
    t = CopyTable(self)
    t.key = key
    t.msgid = msgid
    t.value = value
    EnumBox:Add(t)
EndFunction       
 
Function EnumBox:Add(entry)
    ;check if already exists
    Local ret = EnumBox:CheckExist(entry)
    If(ret = 0)
       InsertItem(self.storage, entry)
    EndIf()
EndFunction

Function EnumBox:CheckExist(entry)
    Local a, b = NextItem(EnumBox.storage)
    While GetType(a) <> #NIL
      ;DebugPrint(b.key.."--"..b.msgid.."--"..b.value)
      If(entry.key = b.key and entry.msgid = b.msgid and entry.value = b.value)
         Return(1)
      EndIf()
      a, b = NextItem(EnumBox.storage, a)
    Wend
    Return(0)
EndFunction       
Is there a nicer way to check if a tableentry is already saved within a table?
Can the statement

Code: Select all

t = CopyTable(self)
be avoided?

Re: Inspecting nested tables

Posted: Fri Apr 07, 2017 4:17 pm
by p-OS
Maybe you could post an example of how your table of enums shall lool like in the end ?

Re: Inspecting nested tables

Posted: Sun Apr 09, 2017 3:03 pm
by airsoftsoftwair
I don't know what exactly the OP is trying to do here either... more details, please...

Re: Inspecting nested tables

Posted: Sun Apr 09, 2017 10:17 pm
by p-OS
He tries to simulate ENUMs from Java. But his code might be little bit ineffective ;)

Re: Inspecting nested tables

Posted: Mon Apr 10, 2017 11:18 am
by GMKai
Yes indeed,
idea is to get something like a java-enum.
The storage is:
EnumBox = {storage = {}}
EnumEntry = {key ="",msgid="",value=""}

where "EnumBox" holds all Enums that can be defined during runtime.
A single EnumEntry can be compared to a constant,

What I hope to achieve is a way to handle complex situations with (extended) key-value pairs.
In the code one can handle numerical comparisons, and with some extrainformation one can do something more, like inform a user or do followup-calculation,

When an entry represents an event/situation one can lookup msgid and value and decide what to do.

Re: Inspecting nested tables

Posted: Tue Apr 11, 2017 11:50 am
by airsoftsoftwair
Well, I don't know anything about Java enums but it should be possible to emulate those in Hollywood. Maybe this link helps: http://unendli.ch/posts/2016-07-22-enum ... n-lua.html