Hash Compare

Johnny bierbauch at jenz.info
Wed Apr 18 10:24:56 PDT 2007


BCS Wrote:

> 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)
> }

A good hint. my problem is that I get an ArrayBounds error if i try it with my method.
... an I can't catch it


More information about the Digitalmars-d-learn mailing list