Just starting out? Need help? Post your questions and find answers here.
Quin
Addict
Posts: 1135 Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:
Post
by Quin » Sat Apr 12, 2025 8:00 pm
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.
mk-soft
Always Here
Posts: 6315 Joined: Fri May 12, 2006 6:51 pm
Location: Germany
Post
by mk-soft » 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()
skywalk
Addict
Posts: 4241 Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA
Post
by skywalk » Sat Apr 12, 2025 9:00 pm
You need to do a deep copy.
*Out\Num = Things()\Num
*Out\Str = Things()\Str
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
Quin
Addict
Posts: 1135 Joined: Thu Mar 31, 2022 7:03 pm
Location: Colorado, United States
Contact:
Post
by Quin » Sat Apr 12, 2025 9:08 pm
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.