Tables

Find quick help here to get you started with Hollywood
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Tables

Post by oceanarts »

I want to use a table to store a custom palette with hex values and instead of referencing their positions in the table call them by their variable names, because it's a little easier to remember names.

I tried:

Code: Select all

_COLS = {[Yel] = $FFD300, [Rd] = $FF4C4C, [Gr] = $3FB63F, [Bl] = $53ACE4}

Function p_testscreen()

	Local display_w = GetAttribute(#DISPLAY, 1, #ATTRWIDTH)
	Local display_h = GetAttribute(#DISPLAY, 1, #ATTRHEIGHT)

	SetFont(#MONOSPACE, 30)
        SetFontStyle(#ANTIALIAS)
        SetFontColor(_GREYS[9])
	TextOut(#CENTER, #CENTER, "Hello World!")
	Line(0, 0, display_w, display_h, _COLS[Gr])
	Line(display_w, 0, 0, display_h, _COLS[Gr])
	Box(0, 0, display_w, display_h, _COLS[Yel])
	Box(#CENTER, #CENTER, display_w / 2, display_h / 2, _COLS[Rd])
	Circle(#CENTER, #CENTER, display_h / 8, _COLS[Bl])

EndFunction
Which runs, but only the Bl variable is used. I thought this would work, what is the proper way to do this?

Thanks!
Development System : Imac G5, MorphOs 3.19
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Tables

Post by Flinx »

Bad table definition, look at the examples in the doc. And use lower case indices; read about this in the description of the RawGet() command.

Code: Select all

_COLS = {yel = $FFD300, rd = $FF4C4C, gr = $3FB63F, bl = $53ACE4} ; Syntax without brackets

ForEach(_COLS, DebugPrint) ; to see what table you have created

Function p_testscreen()

	Local display_w = GetAttribute(#DISPLAY, 1, #ATTRWIDTH)
	Local display_h = GetAttribute(#DISPLAY, 1, #ATTRHEIGHT)

	SetFont(#MONOSPACE, 30)
        SetFontStyle(#ANTIALIAS)
        ;SetFontColor(_GREYS[9]) ; missing in your example
	TextOut(#CENTER, #CENTER, "Hello World!")
	Line(0, 0, display_w, display_h, _COLS["gr"]) ; lower case
	Line(display_w, 0, 0, display_h, _COLS["gr"])
	Box(0, 0, display_w, display_h, _COLS["yel"])
	Box(#CENTER, #CENTER, display_w / 2, display_h / 2, _COLS["rd"])
	Circle(#CENTER, #CENTER, display_h / 8, _COLS["bl"])

EndFunction

p_testscreen()
WaitLeftMouse
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Aha! Thanks. I associate quotation marks with strings, so I omitted them. Nothing like an easy fix. :D
Development System : Imac G5, MorphOs 3.19
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Tables

Post by Flinx »

These are strings. :)
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Flinx wrote: Wed Jun 18, 2025 6:14 pm These are strings. :)
Now I'm confused. You don't usually declare a numerical variable in quotations like "bank-balance" = 0, but perhaps that's different in tables.

I actually did it like this:

Code: Select all

;VARS

_GREYS = {$d3d3d3, $bebebe, $a9a9a9, $949494, $7f7f7f, $6a6a6a, $545454, $3f3f3f, $2a2a2a, $151515}
;Yellow, Red, Green, Blue:
_COLS = {["Yel"] = $FFD300, ["Rd"] = $FF4C4C, ["Gr"] = $3FB63F, ["Bl"] = $53ACE4}


@DISPLAY{WIDTH = 1280, HEIGHT = 720, MODE = "WINDOWED", TITLE = "Application Title v1.0", COLOR = $D3D3D3}


EscapeQuit(True)

Function p_testscreen()

	Local display_w = GetAttribute(#DISPLAY, 1, #ATTRWIDTH)
	Local display_h = GetAttribute(#DISPLAY, 1, #ATTRHEIGHT)

	SetFont(#MONOSPACE, 30)
    SetFontStyle(#ANTIALIAS)
    SetFontColor(_GREYS[9])
	TextOut(#CENTER, #CENTER, "Hello World!")
	Line(0, 0, display_w, display_h, _COLS["Gr"])
	Line(display_w, 0, 0, display_h, _COLS["Gr"])
	Box(0, 0, display_w, display_h, _COLS["Yel"])
	Box(#CENTER, #CENTER, display_w / 2, display_h / 2, _COLS["Rd"])
	Circle(#CENTER, #CENTER, display_h / 8, _COLS["Bl"])

EndFunction
Because if I just remove the brackets in the table, then there's an error saying that index 0 wasn't initialized (or something similar).
Development System : Imac G5, MorphOs 3.19
Bugala
Posts: 1390
Joined: Sun Feb 14, 2010 7:11 pm

Re: Tables

Post by Bugala »

Here is the thing.

When you refer to table[index], you are not referring to table.index, but you are instead referring to table[0].

Point here being:

Code: Select all

Index = 5
Table[index] = "a"
and now Table[index] refers to table[5]

If you however use Table["index"] then you are referring to table.index

By otherwords, without quotes around the name, you are not referring to the name, but to the value of a variable.
With Quotes, you are referring to the name.

[index] = what ever value variable index holds
["index"] = refers to tableitem index - table.index


Another thing to notice is in using quotas that there is difference in referring to them.

Code: Select all

tb = {}
tb["gr"] =  1
tb["Gr"] = 2
tb["GR"] = 3

Debugprint(tb["gr"])
Debugprint(tb["Gr"])
Debugprint(tb["GR"])
Debugprint(tb.GR)
End Result, you can notice that gr, Gr, GR are all different table items, each holding a different value, and only referenced when actually using the exact right capital-noncapital combination of letters.

However, when using tb.gr, tb.Gr, or tb.GR, they all reference to the same, tb["gr"] version.

Therefore I would suggest to use tb.gr both when creating them, as well as when referencing to them in the code, so that you dont have to worry about capital-noncapital letters.

As you could for example:

Code: Select all

tb = {gr=1}
debugprint(tb.Gr)
and it would work.

You could also do in such way that when you create the variable use tb.GR method, but when referencing it, use LowerStr() command.

As example:

Code: Select all

tb = {Gr = 1}
Debugprint(  tb[ LowerStr("GR") ]  )
And you would now be referencing tb.gr

Adding LowerStr() makes things less readable, but there are situations where it is useable, for example, you might want for claritys sake use Capital letters sometimes when choosing a Color, but then later need them to access a table, or you might even want them uppercase in some case:

Code: Select all

Color = TextColor
Switch UpperCase(Color)
case GR
case BL
EndSwitch

Code: Select all

ColorToUse = "GR"
ColorToUse = LowerStr(ColorToUse)
TextOut(10, 10, "test", {color=_Cols[ColorToUse]})
Flinx
Posts: 342
Joined: Sun Feb 14, 2021 9:54 am
Location: Germany

Re: Tables

Post by Flinx »

Bugala has already said everything important, so I'll just give another example of how these string indexes can be useful.
My music player has a feature that reads a rating list and enters the ratings into the internal playlist.
The rating list contains file names and a number. If I were to search for the file names for each entry in the playlist using a loop, the import would be very slow.
Therefore, I create a search list with the file names as indexes to quickly find the position. In practice, it looks like this:

Code: Select all

; making the search table for the internal playlist gPlayList:
Local PlFileNameIndex={}
For Local i=0 To ListItems(gPlayList)-1
	PlFileNameIndex[PatternReplaceStr(gPlayList[i][0], "%.%w+$","")]=i ; (the regex removes the extension)
Next 

...
; searching one track:
Local trackwox$= PatternReplaceStr(track$, "%.%w+$","") ; (track$ is the filename)
Local tpos=RawGet(PlFileNameIndex,  trackwox$) ; tpos is the needed position in gPlayList
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Think I get it. At least partially.
Guess I have to learn how to use tables in small steps. Seems very powerful.

Thanks for all the information, guys. Appreciate it.
Development System : Imac G5, MorphOs 3.19
oceanarts
Posts: 111
Joined: Mon May 27, 2024 10:42 pm

Re: Tables

Post by oceanarts »

Could a function get its name from a string variable inside a table?
Like:

p_table["name"]()

or something?
Development System : Imac G5, MorphOs 3.19
User avatar
emeck
Posts: 191
Joined: Fri Apr 03, 2015 3:17 pm

Re: Tables

Post by emeck »

@oceanarts

You can have functions as table members. See https://www.hollywood-mal.com/docs/html ... Table.html.

So you can call those functions using either p_table["name"]() or p_table.name().
PowerBook 5.8 MorphOS 3.18
Mac Mini MorphOS 3.18
Post Reply