Iteration through lists and maps

Got an idea for enhancing PureBasic? New command(s) you'd like to see?
uwekel
Enthusiast
Enthusiast
Posts: 740
Joined: Sat Dec 03, 2011 5:54 pm
Location: Oldenburg (Germany)

Iteration through lists and maps

Post by uwekel »

Hi,

to iterate through lists or maps, we do

Code: Select all

ForEach MyList()
  ;here you must access the item with prefix MyList()
Next
If the list variable has a long name, it would be very convenient to have a syntax like

Code: Select all

ForEach i In MyList()
  ;from here you can access the list item just with the prefix i
Next
I know we have the With statement, but it cannot be nested into another With statement. Of course, we can assign the MyList() afterwards e.g. with

Code: Select all

*i.MyStructure = MyList()
but this requires another line of code.

Best regards
Uwe
PB 5.70 LTS (x64) - Debian Testing, Gnome 3.30.2
User avatar
STARGÅTE
Addict
Addict
Posts: 2227
Joined: Thu Jan 10, 2008 1:30 pm
Location: Germany, Glienicke
Contact:

Re: Iteration through lists and maps

Post by STARGÅTE »

you can re-define the ForEach ^^

Code: Select all

Macro ForEachEx(List, As)
	ForEach List : As = @List
EndMacro

Macro ForEach(List, As)
	ForEachEx(List, As)
EndMacro




NewList MyList.i()

AddElement(MyList()) : MyList() = 123

ForEach(MyList(), *i.Integer)
	Debug *i\i
Next
PB 6.01 ― Win 10, 21H2 ― Ryzen 9 3900X, 32 GB ― NVIDIA GeForce RTX 3080 ― Vivaldi 6.0 ― www.unionbytes.de
Lizard - Script language for symbolic calculations and moreTypeface - Sprite-based font include/module
Post Reply