digitalmars.D.learn - Hash Compare
- Johnny (16/16) Apr 18 2007 Hello,
- BCS (9/31) Apr 18 2007 What is the issue? That's about as good as anything I can think of. You
- Johnny (3/36) Apr 18 2007 A good hint. my problem is that I get an ArrayBounds error if i try it w...
- Derek Parnell (9/30) Apr 18 2007 Try replacing "if (b[m])" with "if (m in b)".
Hello, I've got a problem. I need to check if a hash key exists in a for loop like: int [char[]] a; int [char[]] b; a = { a => 1; b => 5; c=> 8 } b = { a => 5; b => 4; d => 3; f => 4 } foreach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } } ... not "real" D but I hope you understand the code and what it should do. Thanks.
Apr 18 2007
Johnny wrote:Hello, I've got a problem. I need to check if a hash key exists in a for loop like: int [char[]] a; int [char[]] b; a = { a => 1; b => 5; c=> 8 } b = { a => 5; b => 4; d => 3; f => 4 } foreach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } } ... not "real" D but I hope you understand the code and what it should do. Thanks.What is the issue? That's about as good as anything I can think of. You might try this (but it's not much better) foreach (k, v1; a) { if ( auto v2 = k in b ) // is this what your looking for? writefln("%s %s %s", k, v1, *v2) else writefln("%s %s", k, v1) }
Apr 18 2007
BCS Wrote:Johnny wrote:A good hint. my problem is that I get an ArrayBounds error if i try it with my method. ... an I can't catch itHello, I've got a problem. I need to check if a hash key exists in a for loop like: int [char[]] a; int [char[]] b; a = { a => 1; b => 5; c=> 8 } b = { a => 5; b => 4; d => 3; f => 4 } foreach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } } ... not "real" D but I hope you understand the code and what it should do. Thanks.What is the issue? That's about as good as anything I can think of. You might try this (but it's not much better) foreach (k, v1; a) { if ( auto v2 = k in b ) // is this what your looking for? writefln("%s %s %s", k, v1, *v2) else writefln("%s %s", k, v1) }
Apr 18 2007
Johnny wrote:BCS Wrote:The "/a/ in /b/" construct fixes that. Just looking up the value should do what you described if the value isn't their.Johnny wrote:A good hint. my problem is that I get an ArrayBounds error if i try it with my method. ... an I can't catch itforeach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } }foreach (k, v1; a) { if ( auto v2 = k in b ) // is this what your looking for? writefln("%s %s %s", k, v1, *v2) else writefln("%s %s", k, v1) }
Apr 18 2007
BCS Wrote:Johnny wrote:I've tested it, and yes thank you very much it works fine! .. but still don't really understand what "auto v2 = m in b" means something like v2 = b.keys ?BCS Wrote:>>Johnny wrote:The "/a/ in /b/" construct fixes that. Just looking up the value should do what you described if the value isn't their.A good hint. my problem is that I get an ArrayBounds error if i try it with my method. ... an I can't catch itforeach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } }foreach (k, v1; a) { if ( auto v2 = k in b ) // is this what your looking for? writefln("%s %s %s", k, v1, *v2) else writefln("%s %s", k, v1) }
Apr 18 2007
Johnny wrote:BCS Wrote:actualy its about three cool features in one line here is the slightly expanded vertion foreach (k, v1; a) { int* v2 = k in b; // return a pointer to the value assigned to key k in b if (v2 !is null ) // if pointer is not null (key exists) writefln("%s %s %s", k, v1, *v2) else // if pointer is null (key dosn't exist) writefln("%s %s", k, v1) } the vertion I used uses type inference to avoid needing to know the type and also uses the "declare and check a variable" feature. http://www.digitalmars.com/d/expression.html#InExpression http://www.digitalmars.com/d/statement.html#IfStatement look for IfCondition and DeclaratorJohnny wrote:I've tested it, and yes thank you very much it works fine! .. but still don't really understand what "auto v2 = m in b" means something like v2 = b.keys ?BCS Wrote:Johnny wrote:The "/a/ in /b/" construct fixes that. Just looking up the value should do what you described if the value isn't their.A good hint. my problem is that I get an ArrayBounds error if i try it with my method. ... an I can't catch itforeach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } }foreach (k, v1; a) { if ( auto v2 = k in b ) // is this what your looking for? writefln("%s %s %s", k, v1, *v2) else writefln("%s %s", k, v1) }
Apr 18 2007
On Wed, 18 Apr 2007 12:13:39 -0400, Johnny wrote:Hello, I've got a problem. I need to check if a hash key exists in a for loop like: int [char[]] a; int [char[]] b; a = { a => 1; b => 5; c=> 8 } b = { a => 5; b => 4; d => 3; f => 4 } foreach (m; a.keys) { if ( b[m] ) { writefln("%s %s %s", m, a[m], b[m]) } else { writefln("%s %s", m, a[m]) } } ... not "real" D but I hope you understand the code and what it should do. Thanks.Try replacing "if (b[m])" with "if (m in b)". The correct way to find out if a specific key exists in an AA is to use the "in" syntax. -- Derek Parnell Melbourne, Australia "Justice for David Hicks!" skype: derek.j.parnell
Apr 18 2007