assigning values to List() elements

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
User avatar
Blue
Addict
Addict
Posts: 864
Joined: Fri Oct 06, 2006 4:41 am
Location: Canada

assigning values to List() elements

Post 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.
"That's not a bug..." said the programmer. "it's a feature! "
"Oh! I see..." replied the blind man.
User avatar
skywalk
Addict
Addict
Posts: 3972
Joined: Wed Dec 23, 2009 10:14 pm
Location: Boston, MA

Re: assigning values to List() elements

Post by skywalk »

Most of my lists are structured. :wink: :idea:
The nice thing about standards is there are so many to choose from. ~ Andrew Tanenbaum
User avatar
Zebuddi123
Enthusiast
Enthusiast
Posts: 794
Joined: Wed Feb 01, 2012 3:30 pm
Location: Nottinghamshire UK
Contact:

Re: assigning values to List() elements

Post 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
malleo, caput, bang. Ego, comprehendunt in tempore
GPI
PureBasic Expert
PureBasic Expert
Posts: 1394
Joined: Fri Apr 25, 2003 6:41 pm

Re: assigning values to List() elements

Post 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
Post Reply