www.digitalmars.com         C & C++   DMDScript  

D - 'in' would be nice for normal arrays, too...

reply Russell Lewis <spamhole-2001-07-16 deming-os.org> writes:
I'm running across places in my code where I want to test an array for 
the existence of a certain value:

     Foo[] array;
     Foo value;
     // need to check: is value in array?
     if(value in array) // this doesn't work, so far
     {
       ..
     }

Right now, I'm wavering.  In one place, I created an associative array 
of bits, and then I get the "keys" property to get the real values:
     bit[Foo] array;
     Foo value;
     if(value in array) // this works
     {
       ..
     }

In another, I'm thinking of writing an actual search function.

Perhaps a wishlist item, I suppose...
Sep 25 2002
next sibling parent Burton Radons <loth users.sourceforge.net> writes:
Russell Lewis wrote:
 I'm running across places in my code where I want to test an array for 
 the existence of a certain value:
 
     Foo[] array;
     Foo value;
     // need to check: is value in array?
     if(value in array) // this doesn't work, so far
     {
       ..
     }
I agree, this should be valid, even though the implementation will just be a linear search. Marked in TODO.
Sep 25 2002
prev sibling next sibling parent "Walter" <walter digitalmars.com> writes:
I think this is a great and natural idea. Thanks!

"Russell Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3D91E904.2080409 deming-os.org...
 I'm running across places in my code where I want to test an array for
 the existence of a certain value:

      Foo[] array;
      Foo value;
      // need to check: is value in array?
      if(value in array) // this doesn't work, so far
      {
        ..
      }

 Right now, I'm wavering.  In one place, I created an associative array
 of bits, and then I get the "keys" property to get the real values:
      bit[Foo] array;
      Foo value;
      if(value in array) // this works
      {
        ..
      }

 In another, I'm thinking of writing an actual search function.

 Perhaps a wishlist item, I suppose...
Sep 29 2002
prev sibling parent reply "Dario" <supdar yahoo.com> writes:
 I'm running across places in my code where I want to test an array for
 the existence of a certain value:

      Foo[] array;
      Foo value;
      // need to check: is value in array?
      if(value in array) // this doesn't work, so far
      {
        ..
      }

 Right now, I'm wavering.  In one place, I created an associative array
 of bits, and then I get the "keys" property to get the real values:
      bit[Foo] array;
      Foo value;
      if(value in array) // this works
      {
        ..
      }

 In another, I'm thinking of writing an actual search function.

 Perhaps a wishlist item, I suppose...
If it were valid, the "in" sintax wouldn't need to be valid for associative arrays, since you would be able to write "key in assArray.keys;" This should be optimized by the compiler and can decrease the language complexity (and maybe the compiler's one), though increasing the typing. Just thoughts.
Oct 10 2002
parent Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Dario wrote:

 I'm running across places in my code where I want to test an array for
 the existence of a certain value:

      Foo[] array;
      Foo value;
      // need to check: is value in array?
      if(value in array) // this doesn't work, so far
      {
        ..
      }

 Right now, I'm wavering.  In one place, I created an associative array
 of bits, and then I get the "keys" property to get the real values:
      bit[Foo] array;
      Foo value;
      if(value in array) // this works
      {
        ..
      }

 In another, I'm thinking of writing an actual search function.

 Perhaps a wishlist item, I suppose...
If it were valid, the "in" sintax wouldn't need to be valid for associative arrays, since you would be able to write "key in assArray.keys;" This should be optimized by the compiler and can decrease the language complexity (and maybe the compiler's one), though increasing the typing. Just thoughts.
This actually might be a very good idea, since a programmer might want to know if a value is "in keys" or "in values". I would support doing this. Disallow the "in" property for associative arrays, but make an explicit suggestion that the compiler optimize for "value in assocArray.keys" and "value in assocArray.values". -- The Villagers are Online! http://villagersonline.com .[ (the fox.(quick,brown)) jumped.over(the dog.lazy) ] .[ (a version.of(English).(precise.more)) is(possible) ] ?[ you want.to(help(develop(it))) ]
Oct 11 2002