Page 1 of 1

Assigning a pointer to a structure passed as a parameter to the current value of a list?

Posted: Sat Apr 12, 2025 8:00 pm
by Quin
I have this test code:

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
			*Out = Things()
			ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
Print("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()
However, if I type Test1 or Test2 in any case, it always shows 0 and doesn't have the correct values assigned to the fields, but it does successfully find it. If I type an invalid name, the invalid name handling triggers. This is utterly baffling.

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Posted: Sat Apr 12, 2025 8:46 pm
by mk-soft

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
		  ;*Out\Num = Things()\Num
		  ;*Out\Str = Things()\Str
		  CopyStructure(@Things(), *Out, Thing)
		  ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
PrintN("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + "," + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Posted: Sat Apr 12, 2025 9:00 pm
by skywalk
You need to do a deep copy.
*Out\Num = Things()\Num
*Out\Str = Things()\Str

Re: Assigning a pointer to a structure passed as a parameter to the current value of a list?

Posted: Sat Apr 12, 2025 9:08 pm
by Quin
mk-soft wrote: Sat Apr 12, 2025 8:46 pm

Code: Select all

EnableExplicit

Structure Thing
	Str.s
	Num.i
EndStructure

Global NewList Things.Thing()

Macro AddThing(_Str_, _Num_)
	AddElement(Things())
	Things()\Str = _Str_
	Things()\Num = _Num_
EndMacro

AddThing("Test1", 12)
AddThing("Test2", 14)

Procedure.i FindThingByStr(Str$, *Out.Thing)
	If Str$ = "" : ProcedureReturn #False : EndIf
	Str$ = LCase(Str$)
	ForEach Things()
		If LCase(Things()\Str) = Str$
		  ;*Out\Num = Things()\Num
		  ;*Out\Str = Things()\Str
		  CopyStructure(@Things(), *Out, Thing)
		  ProcedureReturn #True
		EndIf
	Next
	ProcedureReturn #False
EndProcedure

OpenConsole()
PrintN("Type a name: ")
Define Name$ = Input()
Define T.Thing
If FindThingByStr(Name$, @T)
	PrintN(T\Str + "," + T\Num)
Else
	PrintN(Name$ + " not found.")
EndIf
Input()
Thanks a ton, this works! I could've sworn there was a CopyStructure() function, but I couldn't find it under the Memory section of the documentation and didn't search that well.