Use of Tables

Find quick help here to get you started with Hollywood
Post Reply
SpawnPPC
Posts: 61
Joined: Fri Mar 15, 2024 11:51 pm

Use of Tables

Post by SpawnPPC »

I tried to use a single table but I failed. As a result I always get an initialization error and I don't understand why and I would really like to learn how to use tables. However, if I make many single tables everything works, so in reality my problem only occurs when I try to make a single table. I would like to make a sort of matrix, with rows and columns. The columns will have a name in the first field at the top, such as "type of operating system" for the first column, "abbreviation" in the second column, "specific system" in the third column. Obviously, abbreviations of these names or an acronym that suits the task in question are also fine. Then from the next position put the values ​​based on the name of the column. Here, I didn't succeed. The code that I would like to transform into a table is the following. Any suggestions?

Thanks to everyone as always!!!

Emanuele

-----------

Code: Select all

Function HostSystem()
    If GetVersion().Platform = "AmigaOS3"
        SystemType = "AOS"
        SystemSpecific = "AOS3"
    ElseIf GetVersion().Platform = "AmigaOS4"
        SystemType = "AOS"
        SystemSpecific = "AOS4"
    ElseIf GetVersion().Platform = "AROS"
        SystemType = "AOS"
        SystemSpecific = "AROS"
    ElseIf GetVersion().Platform = "MorphOS"
        SystemType = "AOS"
        SystemSpecific = "MorphOS"
    ElseIf GetVersion().Platform = "MacOS"
        SystemType = "MacOS"
        SystemSpecific = "MacOS"
    ElseIf GetVersion().Platform = "Win32"
        SystemType = "W"
        SystemSpecific = "Win32"
    Else
        SystemType = "Unknown"
        SystemSpecific = "Unknown"
    EndIf
    DebugPrint("Detected system type:", SystemType)
    DebugPrint("Detected specific system:", SystemSpecific)
EndFunction
plouf
Posts: 608
Joined: Sun Feb 04, 2018 11:51 pm
Location: Athens,Greece

Re: Use of Tables

Post by plouf »

examples/description exist in manual -> https://www.hollywood-mal.com/docs/html ... Table.html
examples in lua manual -> https://www.lua.org/pil/11.2.html

tables in hollywood and lua, are practically only arrays
manual says to either use an array in each element like

Code: Select all

 	
buts = { {x1 = 0, y1 = 0, x2 = 100, y2 = 50},
    {x1 = 100, y1 = 0, x2 = 80, y2 = 50},
    {x1 = 180, y1 = 0, x2 = 100, y2 = 50} }
as you see its an array where each element is a 4 element array, so its "3x4 table"

OR

Code: Select all

N = 50
M = 100
mtx = {}                            ; create an empty table
For i = 0 To N - 1
    mtx[i] = {}                     ; create a new row
    For j = 0 To M - 1
            mtx[i][j] = 0           ; initialize element
    Next
Next
above, manual, again example creates a 50x100=5000 array items, but a loop creates it

OR

Code: Select all

mt = {}  
        
For i=1 To 9 
 b=b+1  
 mt[i] = b
Next


For i=1 To 3
	For b=1 To 3
		NPrint("row "..StrStr(i).." column "..StrStr(b) .." = "..mt[i*b])
	Next
Next

WaitLeftMouse()
also you can use a multiplication in index to manually calculate "matrix index" which is the easiest to my mind
Christos
Bugala
Posts: 1300
Joined: Sun Feb 14, 2010 7:11 pm

Re: Use of Tables

Post by Bugala »

I would suggest following:

You can either make the table manually, like this:

Code: Select all

t_HostSystems = { ["AmigaOS3"] = {SystemType = "AOS", SystemSpecific = "AOS3"},
		  ["AmigaOS4"] = {SystemType = "AOS", SystemSpecific = "AOS4"}
		}
Or you could use a function that will make you this table (both have their own benefits):

Code: Select all

t_HostSystems = {}

Function AddHostSystem(versionname, SystemType, SystemSpecific)
	t_HostSystems[versionname] = {SystemType = SystemType, SystemSpecific = SystemSpecific}
EndFunction

AddHostSystem("AmigaOS3", "AOS", "AOS3")
AddHostSystem("AmigaOS4", "AOS", "AOS4")
AddHostSystem("AROS", "AOS", "AROS")
AddHostSystem("MorphOS", "AOS", "MorphOS")
AddHostSystem("MacOS", "MacOS", "MacOS")
AddHostSystem("Win32", "W", "Win32")

Local Version = GetVersion().Platform
Local SystemType = "Unknown"
Local SystemSpecific = "Unknown"


If RawGet(t_HostSystems, Version)
	SystemType = t_Hostsystems[Version].SystemType
	SystemSpecific = t_HostSystems[Version].SystemSpecific
EndIf


DebugPrint("Detected system type:", SystemType)
DebugPrint("Detected specific system:", SystemSpecific)
Idea is that you store GetVersion().Platform result into "Version" variable.

Then you will initialize SystemType and SystemSpecific as "Unknown" at first.

Using the "Version" variable, you will now use RawGet to see, if this version has an item in the t_HostSystems table, if it does, then it will assign that entrys SystemType and SystemSpecific instead of the "Unknown" ones.

This way, if item is not found, it will stay as "unknown", but if they do, then they will get initialized something else in them.


One more notice, basically you should be able to use HasItem or HaveItem instead of RawGet, but for some reason it didn't work when I tried, might be a bug in Hollywood.
Flinx
Posts: 273
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Use of Tables

Post by Flinx »

Bugala wrote: Mon Jan 20, 2025 1:24 pm basically you should be able to use HasItem or HaveItem instead of RawGet, but for some reason it didn't work when I tried, might be a bug in Hollywood.
No bug, HaveItem/HasItem expect lowercase indices.
Bugala
Posts: 1300
Joined: Sun Feb 14, 2010 7:11 pm

Re: Use of Tables

Post by Bugala »

After Flinx pointed out the difference between RawGet and HaveItem/HasItem, if you want to rather use HasItem/HaveItem option, then you could change the program a bit:

Code: Select all

Function AddHostSystem(versionname, SystemType, SystemSpecific)
	t_HostSystems[LowerStr(versionname)] = {SystemType = SystemType, SystemSpecific = SystemSpecific}
EndFunction

AddHostSystem("AmigaOS3", "AOS", "AOS3")
AddHostSystem("AmigaOS4", "AOS", "AOS4")
AddHostSystem("AROS", "AOS", "AROS")
hha
AddHostSystem("MorphOS", "AOS", "MorphOS")
AddHostSystem("MacOS", "MacOS", "MacOS")
AddHostSystem("Win32", "W", "Win32")

Local Version = GetVersion().Platform
Version = LowerStr(Version)
Local SystemType = "Unknown"
Local SystemSpecific = "Unknown"


If HasItem(t_HostSystems, Version)
	SystemType = t_Hostsystems[Version].SystemType
	SystemSpecific = t_HostSystems[Version].SystemSpecific
EndIf


DebugPrint("Detected system type:", SystemType)
DebugPrint("Detected specific system:", SystemSpecific)
There are two differences in this:

Code: Select all

t_HostSystems[versionname] = {SystemType = SystemType, SystemSpecific = SystemSpecific}
has been changed into:

Code: Select all

t_HostSystems[LowerStr(versionname)] = {SystemType = SystemType, SystemSpecific = SystemSpecific}
Only difference is that instead of "versionname" it will first use "LowerStr()" command to make the versionname into lower letters (Ie. "AbCdE", changed into "abcde"), which means that instead of having t_HostSystems["AmigaAOS3"] = {} it will now instead have t_HostSystems["amigaaos3"] = {}

Another change is that after taking

Code: Select all

Local Version = GetVersion().Platform
there is additional line of:

Code: Select all

Version = LowerStr(Version)
Meaning that when GetVersion gives Version something like "AmigaAOS3" it will change it into "amigaaos3".

reason for this is since most of time when you use Hollywood to search for tables "variablename" indices, like table["Item"], it will do so by using lower letters, but not always.

Sometimes it might treat "ITEM" and "item" as one and same, and sometimes it thinks them as two different things.

This is why RawGet() was working and HasItem/HaveItem didn't in the original example.

RawGet was able to look for "AmigaAOS3" item specifically, while HasItem/HaveItem weren't able to look for "AmigaAOS3".

If you remove the line "Version = LowerStr(Version)", then you will encounter another problem.

For without that line we are at situation where the Table Item is:
"t_HostSystems["amigaaos3"]"
and the "Version" is "AmigaAOS3".

When using HasItem/HaveItem on the IF check:

Code: Select all

If HasItem(t_HostSystems, Version)
it actually works right, since HasItem/HaveItem will automatically be looking for lower letters version. That while "Version" is "AmigaAOS3" HasItem/HaveItem is actually looking for "amigaaos3" instead, which matches with the current t_HostSystems item naming.

However, when inside that IF loop:

Code: Select all

If HasItem(t_HostSystems, Version)
	SystemType = t_Hostsystems[Version].SystemType
	SystemSpecific = t_HostSystems[Version].SystemSpecific
EndIf
problem is that at point "SystemType = t_Hostsystems[Version].SystemType" Hollywood is looking for "AmigaAOS3" entry, and cant find it, because there is only "amigaaos3" entry existing.

Due to Hollywood in some cases being case insensitive, and sometimes case sensitive, I recommend doing it this way that I am showing now, as to show everything into lower letters, since that is the way to cause least problems.

Notice to use lowercase letters, not uppercase letters, because once again, if you have entry t_HostSystems["AMIGAAOS3"], and then you try using HasItem/HaveItem with "AMIGAAOS3", it will be looking for "amigaaos3" instead, and fail, since it cant find one.

Also, I recommend using HasItem/HaveItem instead of RawGet, simply because if you do some error in the code where you are accidentally using UpperCase letters, like "AmigaAOS3", then HasItem/HaveItem has better chance to fail and you can notice that error.

RawGet is basically quite dangerous to use, since without doing any changes into lower or uppercase letters, you can in the worse case have a mix of lowercase and uppercase letters. As in, You have Item "AmigaAOS3", which needs to be searched precisely with "AmigaAOS3".
For example, if Hollywoods next version decides to change the naming to "AmigaAos3" instead, then RawGet will fail, since it cant find that entry.

Instead, if you are doing like I have shown in this example, as in changing everything to lower letter, then even if Hollywood would change "AmigaAOS3" to "AmigaAos3", it would still work as usual.

Also notice that when making the t_HostSystems table, I am doing the change to Lower Letters inside the Function:

Code: Select all

Function AddHostSystem(versionname, SystemType, SystemSpecific)
	t_HostSystems[versionname] = {SystemType = SystemType, SystemSpecific = SystemSpecific}
EndFunction
This is so that when I am using the "AddHostSystem" command, I don't need to worry about Uppercase or Lowercase letters, but can simply use "AmigaAOS3" which makes it easier to read the code, although inside the function this is changed into "amigaaos3".

Similarly when you get the GetVersion().Platform, remember to change it instantly into lowercase version to avoid any problems. And Similarly if you for some reason want to manually compare for example the "AmigaAOS3", you could do it this way:

Code: Select all

IF Lowerstr("AmigaAOS3") = Version
and this way be able to yourself write a mix of upper and lowercase letters, even Hollywood would be dealing with lowercase versions in reality.

To put it short, always use lowercase letters to minimize the troubles coming from upper/lowercase letter conflicts.
SpawnPPC
Posts: 61
Joined: Fri Mar 15, 2024 11:51 pm

Re: Use of Tables

Post by SpawnPPC »

I understand and thnx to all.

It work.

Maybe my table wasn't correctly initialized.

Thanx very much !

Emanuele
Post Reply