Page 1 of 1

assigning values to List() elements

Posted: Sun Apr 23, 2017 10:37 pm
by Blue
I wish that PB allowed the following syntax for assigning values to dynamic lists :

Code: Select all

Newlist things.s()
AddElement(things(), "thing #1")
AddElement(things(), "thing #2")
AddElement(things(), "thing #3")

AddElement(things(), "thing #4", "thing #5","thing #6")

Newlist numbers()
AddElement(number(), 1)
AddElement(number(), 4)
AddElement(number(), 7)

AddElement(number(), 33, 55, 77)


instead of having to write

Code: Select all

Newlist things.s()
AddElement(things())
things() = "thing #1"
AddElement(things())
things() = "thing #2"
AddElement(things())
things() = "thing #3"
[etc...]

Newlist numbers()
AddElement (numbers())
numbers() = 1
AddElement (numbers())
numbers() = 2
[etc...]

In my view, which I share :D , not only would it make code simpler to write and read, it would also make PB more intuitive.

Re: assigning values to List() elements

Posted: Sun Apr 23, 2017 11:31 pm
by skywalk
Most of my lists are structured. :wink: :idea:

Re: assigning values to List() elements

Posted: Mon Apr 24, 2017 1:20 am
by Zebuddi123
Hi Blue +1 Kind of a Python Tuple would be nice Though I imagine it would be a complex task to implement if at all possible with using structured variables etc in pb . For simple variables, here is a macro I use for strings though I suppose it would be fairly easy to implement a pseudo auto cast for primitive variables. Anyway`s this macro works with lists, maps, and array. For maps the mapkey parameter is checked for the same count as the variable parameter. Maybe be of use or interest to some. :D

Zebuddi.

Code: Select all

Enumeration	
	#TupleArray
	#TupleList
	#TupleMap
EndEnumeration 

NewList ThisList$()
NewMap ThisMap$()
Dim TupleArray$(0)

Macro Tuple(type, ObjectName, paramaters, mapkeylist)
	TupleCountList 		= CountString(paramaters, Chr(44)) + 1
	TupleCountMapList 	= CountString(mapkeylist, Chr(44)) + 1
CompilerSelect  type
	
CompilerCase  #TupleArray
	ReDim ObjectName(TupleCountList - 1)  ; Just the array name needed no parenthesis for redim
	For TupleiIndex = 0 To TupleCountList - 1 	
		TupleParamater$ = StringField(paramaters, TupleiIndex + 1, Chr(44))
		ObjectName(TupleiIndex) = TupleParamater$
	Next
CompilerCase #TupleList
	For TupleiIndex = 0 To TupleCountList - 1 	
		TupleParamater$ = StringField(paramaters, TupleiIndex + 1, Chr(44)) 
		Debug TupleParamater$
		AddElement(ObjectName): ObjectName = TupleParamater$
	Next
CompilerCase #TupleMap
	If TupleCountList = TupleCountMapList
		For TupleiIndex = 0 To TupleCountList - 1 	
			TupleParamater$	= StringField(paramaters, TupleiIndex + 1, Chr(44))
			ListMapKey$		= StringField(mapkeylist, TupleiIndex + 1, Chr(44)) 
			AddMapElement(ObjectName, ListMapKey$): ObjectName = TupleParamater$
		Next
	EndIf
CompilerEndSelect	
EndMacro


Tuple(#TupleList, 	ThisList$(), "hello, world, this, is , a tuple, world", "")
Tuple(#TupleMap,	ThisMap$() , "hello, world, this, is , a tuple, world" ,"h, w, t, i, a, w") 
Tuple(#TupleArray, 	TupleArray$, "hello, world, this, is , a tuple, world", "") ; ; Just the array name needed no parenthesis for redim

CallDebugger

Re: assigning values to List() elements

Posted: Tue Apr 25, 2017 1:09 pm
by GPI
this would be my solution for this problem:

Code: Select all

Macro InsertValue(LList,value)
  InsertElement(llist)
  llist=value
EndMacro

; "with" is forbidden in macros.
Macro InsertValues(LList,v1,v2=,v3=,v4=,v5=,v6=,v7=,v8=,v9=,v10=)
  InsertElement(LList)
  LList#v1
  LList#v2
  LList#v3
  LList#v4
  LList#v5
  LList#v6
  LList#v7
  LList#v8
  LList#v9
  LList#v10
EndMacro

NewList iTest.i()
InsertValue(iTest(),1)
InsertValue(iTest(),3)
InsertValue(iTest(),6)
InsertValue(iTest(),7)
Debug "iTest"
ForEach iTest()
  Debug itest()
Next
Debug "--"


NewList sTest.s()
InsertValue(sTest(),"h")
InsertValue(sTest(),"o")
InsertValue(sTest(),"l")
InsertValue(sTest(),"e")
Debug "sTest"
ForEach sTest()
  Debug stest()
Next
Debug "--"

Structure account
  name.s
  value.i
EndStructure
NewList aTest.account()
InsertValues(aTest(),\name="Hugo",\value=100)
InsertValues(aTest(),\name="Anna",\value=400)
Debug "aTest"
ForEach aTest()
  Debug aTest()\name+" "+aTest()\value
Next
only problem, the LList in InserValues without a value generate programcode...

edit:
little Variant of InsertValues

Code: Select all

    
    Macro MacroQuote
      "
    EndMacro
    
    ; "with" is forbidden in macros.
    Macro InsertValues(LList,f1,v1,f2=,v2=,f3=,v3=,f4=,v4=,f5=,v5=)
      InsertElement(LList)
      CompilerIf MacroQuote#f1#MacroQuote<>""
        LList\f1=v1
      CompilerEndIf
      CompilerIf MacroQuote#f2#MacroQuote<>""
        LList\f2=v2
      CompilerEndIf
      CompilerIf MacroQuote#f3#MacroQuote<>""
        LList\f3=v3
      CompilerEndIf
      CompilerIf MacroQuote#f4#MacroQuote<>""
        LList\f4=v4
      CompilerEndIf
      CompilerIf MacroQuote#f5#MacroQuote<>""
        LList\f5=v5
      CompilerEndIf
    EndMacro

    Structure account
      name.s
      value.i
    EndStructure
    NewList aTest.account()
    InsertValues(aTest(),name,"Hugo",value,100)
    InsertValues(aTest(),name,"Anna",value,400)
    Debug "aTest"
    ForEach aTest()
      Debug aTest()\name+" "+aTest()\value
    Next