how to filter associative arrays with foreach ?

frame frame86 at live.com
Mon Jun 21 08:35:19 UTC 2021


On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote:
> I often need to iterate through a filtered collection 
> (associative array) as following:
>
> ```d
> string strComputerIDunwanted = "WS2"; /// associative array key 
> to exclude
>
> foreach (strComputerID, udtComputer; udtComputers) { /// 
> .remove!(a => a == strComputerIDunwanted) ... ?
>
>    if (strComputerID != strComputerIDunwanted) {
>
>       ...
>
>    }
>
> }
> ```
>
> Is there a way to filter the collection at the foreach-level to 
> avoid the inner if ?

An associative array is not a range but a struct, so it is extra 
work to create a range from the AA to apply range functions.

You can get a range from it by using something like 
std.array.byPair() but for this usage you would be better of with 
your own function or template.




More information about the Digitalmars-d-learn mailing list