Table with index and string

Find quick help here to get you started with Hollywood
Post Reply
DieterG
Posts: 20
Joined: Tue Oct 23, 2018 9:40 pm

Table with index and string

Post by DieterG »

The documentation shows an easy way to „name“ an index.
And use it in an easier way.
This looks good for me but i do not really understand how it works with 2 or more indexes.
To understand, you may wrote:
mytable[index1,index2]
Or eg
mytable[„name“,“Header“]
Or
mytable.name.Header ?
but is mixing also possible ?
e.g.
mytable[„name“,index2]
or
mytable.name[index2]
Or
mytable[index1].Header

This is not documented, but the last both seems not to work.
If there a way to use it or is there a mistake in my understanding?
Dieter
DieterG
Posts: 20
Joined: Tue Oct 23, 2018 9:40 pm

Re: Table with index and string

Post by DieterG »

Some tests later i think, tables in Hollywood are only with one index possible.
All tries to get good values on 2 dimensions-fields ends with errors or wrong values.
Is this correct ?
Aas i understand the documentation with comma as separator you initialize values.
Maybe there must be an other sign or really not possible ?
The documentation is not very helpful to explain this, please add more basicinfos in it.
Dieter
User avatar
airsoftsoftwair
Posts: 5425
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Table with index and string

Post by airsoftsoftwair »

It's actually not that complicated. Multi-dimensional tables use exactly the same syntax than one-dimensional tables, so you can just do one of those:

Code: Select all

t = {x = {y = 5}}
DebugPrint(t.x.y)
DebugPrint(t["x"]["y"])
DebugPrint(t.x["y"])
DebugPrint(t["x"].y)
The DebugPrint() calls will all print "5"... each line uses a different syntax but all lines will print the "y" field of the subtable "x" inside "t".
DieterG
Posts: 20
Joined: Tue Oct 23, 2018 9:40 pm

Re: Table with index and string

Post by DieterG »

Ok, i wouldn’t define values on tableinit.
So i think the first line must be:
MyTable{{}} for a two dimension table ?
I will try this.
Dieter
DieterG
Posts: 20
Joined: Tue Oct 23, 2018 9:40 pm

Re: Table with index and string

Post by DieterG »

I have tried it, allway fails :cry:
May anybody see the problem and can fix it ?

Code: Select all

Function p_splitsequence()
					;*** find Dataparts and put them into tables ***
	tdata = {{}}		;i need tdata[i,j] wehre i is a string ! This "dobble" from above reply
	templen=MidStr(trans$,2,1,#ENCODING_RAW)				;length vom sequence (more then one sequence may be in one trans$)
	temp$=MidStr(trans$,9+seqlen,-1,#ENCODING_RAW)				;remove header first
	segcount=0
	pos=0									;clear segcounter
	While temp$<>""
		tdata.func[segcount]= "1"			;TEST !!!
		tdata.func[segcount]=MidStr(temp$,0,1)				;FunctionTyp ($15... for reporting HWstate,$6f for ..)
		tdata.artyp[segcount]=MidStr(temp$,1,1)				;ArgTyp (§30=Argument)
		tdata.flen[segcount]=MidStr(temp$,2,1)				;full length
		tdata.typ1[segcount]=MidStr(temp$,3,1)				;Typ ($55 for status)
		tdata.arglen1[segcount]=MidStr(temp$,4,1)			;length of argument
		tdata.arg1[segcount]=ByteAsc(temp$,5)				;the argument
		If tdata.arglen1[segcount]>1 Then tdata.arg1[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,5+1)	;the argument are more bytes
		If tdata.arglen1[segcount]>2 Then tdata.arg1[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,5+2)	;the argument are more bytes
		If tdata.arglen1[segcount]>3 Then tdata.arg1[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,5+3)	;the argument are more bytes
		If tdata.arglen1[segcount]>4 Then tdata.arg1[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,5+4)	;the argument are more bytes
		pos=  5 + tdata.arglen1[segment]
		DebugPrint("Arg1=",tdata.arg1)
		If pos<tdata.flen[segcount] 
	 		tdata.typ2[segcount]=MidStr(temp$,pos,1)				;Typ ($55 for status)
			tdata.arglen2[segcount]=MidStr(temp$,pos+1,1)				;length of argument
			tdata.arg2[segcount]=ByteAsc(temp$,pos+2)				;the argument
			If tdata.arglen2[segcount]>1 Then tdata.arg2[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,pos+5+1)	;the argument are more bytes
			If tdata.arglen2[segcount]>2 Then tdata.arg2[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,pos+5+2)	;the argument are more bytes
			If tdata.arglen2[segcount]>3 Then tdata.arg2[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,pos+5+3)	;the argument are more bytes
			If tdata.arglen2[segcount]>4 Then tdata.arg2[segcount]=tdata.arg1[segcount]*256+ByteAsc(temp$,pos+5+4)	;the argument are more bytes
		EndIf
		temp$=MidStr(trans$,pos)
		segcount = segcount + 1	
	Wend	 
EndFunction

in this case the error is: Table field "func" was not initialized!
But i have already tried many things, i can not find out where is the problem.
Dieter
SamuraiCrow
Posts: 475
Joined: Fri May 15, 2015 5:15 pm
Location: Waterville, Minnesota USA

Re: Table with index and string

Post by SamuraiCrow »

Try adding tdata.func = {} before you assign anything to it. Your double nested {{}} has no name nor index number so the inner table is immediately garbage collected.

Allocation of nested tables generally produces messy code so try allocating one table at a time before assigning any members to it just as a style guide.
I'm on registered MorphOS using FlowStudio.
DieterG
Posts: 20
Joined: Tue Oct 23, 2018 9:40 pm

Re: Table with index and string

Post by DieterG »

if i do so, it was easier to make a table for every entry and use them:

Code: Select all

	tdata_func = {}
	tdata_argtyp = {}
	tdata_flen = {}
	tdata_typ1 = {}
	tdata_arglen1 = {}
	tdata_arg1 = {}
	tdata_typ2  = {}			
	tdata_arglen2  = {}
	tdata_arg2  = {}
.....
		tdata_flen[segcount]=ByteAsc(temp$,0)				;full length
		tdata_func[segcount]=MidStr(temp$,1,1,#ENCODING_RAW)		;FunctionTyp ($15... for reporting HWstate,$6f for ..)
		tdata_argtyp[segcount]=MidStr(temp$,3,1,#ENCODING_RAW)		;ArgTyp (§30=Argument)
		tdata_flen[segcount]=ByteAsc(temp$,4)				;full length
.....
Because there make no sense to have a double index anymore.

O.K. This works, so i use it here.
Dieter
Post Reply