Re: how to filter associative arrays with foreach ?

2021-06-23 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:31:16 UTC, Ali Çehreli wrote: Two options for byKey and byKeyValue: import std; void main() { auto aa = [ "WS2" : 42, "WS3" : 43 ]; string strComputerIDunwanted = "WS2"; foreach (key; aa.byKey.filter!(k => k != strComputerIDunwanted)) { writeln(key,

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 15:32:09 UTC, wjoe wrote: something like this ? ``` D import std.array; import std.algorithm; udtComputers.byPair .filter!(p => p.key != strComputerIDunwanted) .each!( (p) { /* foreach body */ } ); ``` This seems really interesting :) Almost

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 22:08:56 UTC, Steven Schveighoffer wrote: It's actually visually shorter than doing the filter. Indeed; in a few very-specific situations I usually write code like this since it allows me to concentrate on the task at hand and not on the details to access the

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 21:00:42 UTC, Elronnd wrote: Here's how I would do it: foreach (k, v; coll) { if (k == unwanted) continue; ... } You still have an if, but the actual loop body doesn't have to be nested, so it's easy to follow the control flow. almost the same

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Steven Schveighoffer via Digitalmars-d-learn
On 6/21/21 5:00 PM, Elronnd wrote: On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: Is there a way to filter the collection at the foreach-level to avoid the inner if ? Here's how I would do it: foreach (k, v; coll) {     if (k == unwanted) continue;     ... } You still have an if,

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Elronnd via Digitalmars-d-learn
On Monday, 21 June 2021 at 03:59:10 UTC, someone wrote: Is there a way to filter the collection at the foreach-level to avoid the inner if ? Here's how I would do it: foreach (k, v; coll) { if (k == unwanted) continue; ... } You still have an if, but the actual loop body doesn't have

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread wjoe via Digitalmars-d-learn
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) { ///

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread someone via Digitalmars-d-learn
On Monday, 21 June 2021 at 08:35:19 UTC, frame wrote: 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

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread frame via Digitalmars-d-learn
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) { ///

Re: how to filter associative arrays with foreach ?

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn
On 6/20/21 8:59 PM, 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 ==