www.digitalmars.com         C & C++   DMDScript  

D - String Comparison and Dynamic Arrays?

reply "Gary" <gedumer bcpl.net> writes:
Walter,

I can successfully use the switch/case construct to compare equality of a
string, but the same can't be said for using the "if" statement. For
example:
  if (a == "Hello") ...
doesn't succeed.
whereas:
  switch (a)
  {
    case "Hello": ...
  }
does work.

I'm also getting an error during compile ('array.length' is not an lvalue)
with the following:
  int[] array;

  array[array.length++] = 0;
  array[array.length++] = 1;
  array[array.length] = 2;

Gary.
Mar 26 2002
next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Gary" <gedumer bcpl.net> wrote in message
news:a7qll4$1870$1 digitaldaemon.com...

 I can successfully use the switch/case construct to compare equality of a
 string, but the same can't be said for using the "if" statement. For
 example:
   if (a == "Hello") ...
 doesn't succeed.
Currently, you cannot use == and friends to compare strings, and arrays in general. You have to use cmp(): if (!cmp(a, "Hello")) ... Hopefully some operator to compare arrays will be added.
 I'm also getting an error during compile ('array.length' is not an lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
This one (and array.length--) I was asking once, but no luck =)
Mar 26 2002
parent "Gary" <gedumer bcpl.net> writes:
Walter... Any comments?

Pavel Minayev <evilone omen.ru> wrote in message
news:a7qnim$19c9$1 digitaldaemon.com...
 "Gary" <gedumer bcpl.net> wrote in message
 news:a7qll4$1870$1 digitaldaemon.com...

 I can successfully use the switch/case construct to compare equality of
a
 string, but the same can't be said for using the "if" statement. For
 example:
   if (a == "Hello") ...
 doesn't succeed.
Currently, you cannot use == and friends to compare strings, and arrays in general. You have to use cmp(): if (!cmp(a, "Hello")) ... Hopefully some operator to compare arrays will be added.
 I'm also getting an error during compile ('array.length' is not an
lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
This one (and array.length--) I was asking once, but no luck =)
Mar 26 2002
prev sibling parent reply "Walter" <walter digitalmars.com> writes:
"Gary" <gedumer bcpl.net> wrote in message
news:a7qll4$1870$1 digitaldaemon.com...
 I'm also getting an error during compile ('array.length' is not an lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
   array[array.length] = 2;
I disabled setting array.length for operators other than assignment because it reallocates the array, which is an expensive operation. Adding array elements one by one, incrementing the array length each time, will have terrible runtime performance. The way to do it is to preallocate your best guess as to what you'll need, stuff the array elements, and then do an assignment to set the final size. If, as you're stuffing array elements, you run out of length, doubling the existing array length is a good strategy.
Mar 26 2002
next sibling parent reply "Gary" <gedumer bcpl.net> writes:
Walter <walter digitalmars.com> wrote in message
news:a7r3am$1gcu$1 digitaldaemon.com...
 "Gary" <gedumer bcpl.net> wrote in message
 news:a7qll4$1870$1 digitaldaemon.com...
 I'm also getting an error during compile ('array.length' is not an
lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
   array[array.length] = 2;
I disabled setting array.length for operators other than assignment
because
 it reallocates the array, which is an expensive operation. Adding array
 elements one by one, incrementing the array length each time, will have
 terrible runtime performance.

 The way to do it is to preallocate your best guess as to what you'll need,
 stuff the array elements, and then do an assignment to set the final size.
 If, as you're stuffing array elements, you run out of length, doubling the
 existing array length is a good strategy.
Exactly... how would you do that?
Mar 26 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Gary" <gedumer bcpl.net> wrote in message
news:a7r5iq$1hg6$1 digitaldaemon.com...
 The way to do it is to preallocate your best guess as to what you'll
need,
 stuff the array elements, and then do an assignment to set the final
size.
 If, as you're stuffing array elements, you run out of length, doubling
the
 existing array length is a good strategy.
Exactly... how would you do that?
array.length = 100; // guess for (i = 0; 1; i++) { c = getinput(); if (!c) break; if (i == array.length) array.length = array.length * 2; array[i] = c; } array.length = i; Picking a good initial guess is an art, but you usually can pick a value covering 99% of the cases. For example, suppose you are gathering user input from the console - it's unlikely to be longer than 80.
Mar 26 2002
next sibling parent reply "Gary" <gedumer bcpl.net> writes:
Walter <walter digitalmars.com> wrote in message
news:a7r7ok$1ing$1 digitaldaemon.com...
 "Gary" <gedumer bcpl.net> wrote in message
 news:a7r5iq$1hg6$1 digitaldaemon.com...
 The way to do it is to preallocate your best guess as to what you'll
need,
 stuff the array elements, and then do an assignment to set the final
size.
 If, as you're stuffing array elements, you run out of length, doubling
the
 existing array length is a good strategy.
Exactly... how would you do that?
array.length = 100; // guess for (i = 0; 1; i++) { c = getinput(); if (!c) break; if (i == array.length) array.length = array.length * 2; array[i] = c; } array.length = i; Picking a good initial guess is an art, but you usually can pick a value covering 99% of the cases. For example, suppose you are gathering user
input
 from the console - it's unlikely to be longer than 80.
WOW!!! That's a far cry from the example in the docs... which is what I was trying to follow. I LIKE IT:) Thanks... Gary.
Mar 26 2002
parent "Walter" <walter digitalmars.com> writes:
"Gary" <gedumer bcpl.net> wrote in message
news:a7r8b3$1j12$1 digitaldaemon.com...
 WOW!!! That's a far cry from the example in the docs... which is what I
was
 trying to follow. I LIKE IT:)
Looks like I should amend the docs <g>.
Mar 26 2002
prev sibling parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
Walter wrote:

 Picking a good initial guess is an art, but you usually can pick a value
 covering 99% of the cases. For example, suppose you are gathering user input
 from the console - it's unlikely to be longer than 80.
How about some sort of array index syntax that automatically doubles the array length if the index is beyond the length? Ok, I know the syntax I have below is terrible, but what if you could write something like: array.length = 100; // guess for(i=0; 1; i++) { c = getinput(); if(!c) break; array.autoextend[i] = c; // if i >= array.length, doubles array.length automatically } array.length = i; This doesn't add any code to the generated output (since we'd have to do the test anyhow), but something like this would allow for a MUCH cleaneer syntax. NOTE: I'm not suggesting that we automatically extend on ALL index operations...only on specially marked ones. Also, you could, potentially, allow an argument on the autoextend to tell the compiler by what factor to increase the length: array.autoextend(1.2)[i] ... // float argument, autoextends by 20% array.autoextend(3)[] ... // int argument, autoextends by tripling length -- The Villagers are Online! 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))) ]
Mar 27 2002
parent reply Russ Lewis <spamhole-2001-07-16 deming-os.org> writes:
     array.autoextend(3)[] ...      // int argument, autoextends by tripling
 length
Oops, meant to type array.autoextend(3)[i] -- The Villagers are Online! 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))) ]
Mar 27 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"Russ Lewis" <spamhole-2001-07-16 deming-os.org> wrote in message
news:3CA1E868.4BF36E49 deming-os.org...
     array.autoextend(3)[] ...      // int argument, autoextends by
tripling
 length
Oops, meant to type array.autoextend(3)[i] -- The Villagers are Online! 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))) ]
Don't push your luck... :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net __________________________________________ Remove _XYZ from my address when replying by mail
Mar 27 2002
prev sibling next sibling parent "Gary" <gedumer bcpl.net> writes:
Also... when do you expect string comparison to be incorporated?

Walter <walter digitalmars.com> wrote in message
news:a7r3am$1gcu$1 digitaldaemon.com...
 "Gary" <gedumer bcpl.net> wrote in message
 news:a7qll4$1870$1 digitaldaemon.com...
 I'm also getting an error during compile ('array.length' is not an
lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
   array[array.length] = 2;
I disabled setting array.length for operators other than assignment
because
 it reallocates the array, which is an expensive operation. Adding array
 elements one by one, incrementing the array length each time, will have
 terrible runtime performance.

 The way to do it is to preallocate your best guess as to what you'll need,
 stuff the array elements, and then do an assignment to set the final size.
 If, as you're stuffing array elements, you run out of length, doubling the
 existing array length is a good strategy.
Mar 26 2002
prev sibling next sibling parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a7r3am$1gcu$1 digitaldaemon.com...

 I disabled setting array.length for operators other than assignment
because
 it reallocates the array, which is an expensive operation. Adding array
 elements one by one, incrementing the array length each time, will have
 terrible runtime performance.
You're probably right... but what about -- and -=? They sound like a replacement for pop_back() of STL containers - a thing that is of common use. Currently, I have to use s.length = s.length - 1; Why not s.length--; ?
Mar 26 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a7rirf$1ofi$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a7r3am$1gcu$1 digitaldaemon.com...
 I disabled setting array.length for operators other than assignment
because
 it reallocates the array, which is an expensive operation. Adding array
 elements one by one, incrementing the array length each time, will have
 terrible runtime performance.
You're probably right... but what about -- and -=? They sound like a replacement for pop_back() of STL containers - a thing that is of common use. Currently, I have to use s.length = s.length - 1; Why not s.length--; ?
Because then I'd be spending my life explaining why ++ is not implemented but -- is ?? <g>
Mar 26 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a7rq5d$1s0b$3 digitaldaemon.com...

 Because then I'd be spending my life explaining why ++ is not implemented
 but -- is ?? <g>
Then, implement ++ as if a new element with default value of desired type is added. That is, for int[], ++ adds -, for float[] it will be NAN etc... the code would be essentially the same as for ~=.
Mar 27 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a7sde6$26vb$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a7rq5d$1s0b$3 digitaldaemon.com...

 Because then I'd be spending my life explaining why ++ is not
implemented
 but -- is ?? <g>
Then, implement ++ as if a new element with default value of desired type is added. That is, for int[], ++ adds -, for float[] it will be NAN etc... the code would be essentially the same as for ~=.
That would work, but then I'm back to the original point that code that uses ++ on the .length most likely has a severe performance problem, so it's discouraged by not supporting ++.
Mar 29 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a82km4$2m2l$2 digitaldaemon.com...

 Then, implement ++ as if a new element with default value of desired
type
 is added. That is, for int[], ++ adds -, for float[] it will be NAN
etc...
 the code would be essentially the same as for ~=.
That would work, but then I'm back to the original point that code that
uses
 ++ on the .length most likely has a severe performance problem, so it's
 discouraged by not supporting ++.
It wouldn't be slower than ~=, right? And ~= is there...
Mar 29 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a82n7g$oq9$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a82km4$2m2l$2 digitaldaemon.com...
 That would work, but then I'm back to the original point that code that
uses
 ++ on the .length most likely has a severe performance problem, so it's
 discouraged by not supporting ++.
It wouldn't be slower than ~=, right? And ~= is there...
Yes, but ~= is useful for concatenating strings with more than one entry. ++ by definition only adds one, and so is the worst case solution :-(
Mar 29 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a82sab$2fjh$1 digitaldaemon.com...

 Yes, but ~= is useful for concatenating strings with more than one entry.
++
 by definition only adds one, and so is the worst case solution :-(
I often use ~= to build arrays element-by-element: char[] token; ... do { token ~= c; in.read(c); } while (isalpha(c)); The same "worst" case. I thought arrays try to compensate it by allocating memory in blocks?
Mar 29 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a83g36$1sns$1 digitaldaemon.com...
 I often use ~= to build arrays element-by-element:
     char[] token;
     ...
     do
     {
         token ~= c;
         in.read(c);
     } while (isalpha(c));

 The same "worst" case. I thought arrays try to compensate it by
 allocating memory in blocks?
That turned out to have problems implementing it. The trouble was if someone pointed to a slice adjacent to it at a higher memory address.
Apr 07 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a8p4l7$105m$1 digitaldaemon.com...

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:a83g36$1sns$1 digitaldaemon.com...
 I often use ~= to build arrays element-by-element:
     char[] token;
     ...
     do
     {
         token ~= c;
         in.read(c);
     } while (isalpha(c));

 The same "worst" case. I thought arrays try to compensate it by
 allocating memory in blocks?
That turned out to have problems implementing it. The trouble was if
someone
 pointed to a slice adjacent to it at a higher memory address.
I can't understand... what do you mean by "adjancent slice"?
Apr 07 2002
parent reply "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8pask$1d5t$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a8p4l7$105m$1 digitaldaemon.com...

 "Pavel Minayev" <evilone omen.ru> wrote in message
 news:a83g36$1sns$1 digitaldaemon.com...
 I often use ~= to build arrays element-by-element:
     char[] token;
     ...
     do
     {
         token ~= c;
         in.read(c);
     } while (isalpha(c));

 The same "worst" case. I thought arrays try to compensate it by
 allocating memory in blocks?
That turned out to have problems implementing it. The trouble was if
someone
 pointed to a slice adjacent to it at a higher memory address.
I can't understand... what do you mean by "adjancent slice"?
char[] a = new char[10]; char[] b = a[5..10]; a.length = 5; a.length = 10; Oops! I just overwrote b!
Apr 07 2002
parent reply "Pavel Minayev" <evilone omen.ru> writes:
"Walter" <walter digitalmars.com> wrote in message
news:a8r7up$2nc$1 digitaldaemon.com...

     char[] a = new char[10];
     char[] b = a[5..10];
     a.length = 5;
     a.length = 10;

 Oops! I just overwrote b!
Hm... I thought that, when length increases, the memory is reallocated!
Apr 08 2002
parent "Walter" <walter digitalmars.com> writes:
"Pavel Minayev" <evilone omen.ru> wrote in message
news:a8rnff$14to$1 digitaldaemon.com...
 "Walter" <walter digitalmars.com> wrote in message
 news:a8r7up$2nc$1 digitaldaemon.com...

     char[] a = new char[10];
     char[] b = a[5..10];
     a.length = 5;
     a.length = 10;
 Oops! I just overwrote b!
Hm... I thought that, when length increases, the memory is reallocated!
Yes, and it must be because of that scenario above.
Apr 12 2002
prev sibling parent reply "Sean L. Palmer" <spalmer iname.com> writes:
Why not just double the darray's allocated space when array.length++ exceeds
the allocated space?  Oh, because you're only storing the length, assuming
the allocated size == the length, always.

Think of it like a std::vector<>::push_back().  Yeah it's inefficient but
sometimes it's the most elegant way to code an algorithm, and not everybody
is always primarily concerned with speed.  Think college D courses.  ;)

Sean

"Walter" <walter digitalmars.com> wrote in message
news:a7r3am$1gcu$1 digitaldaemon.com...
 "Gary" <gedumer bcpl.net> wrote in message
 news:a7qll4$1870$1 digitaldaemon.com...
 I'm also getting an error during compile ('array.length' is not an
lvalue)
 with the following:
   int[] array;

   array[array.length++] = 0;
   array[array.length++] = 1;
   array[array.length] = 2;
I disabled setting array.length for operators other than assignment
because
 it reallocates the array, which is an expensive operation. Adding array
 elements one by one, incrementing the array length each time, will have
 terrible runtime performance.

 The way to do it is to preallocate your best guess as to what you'll need,
 stuff the array elements, and then do an assignment to set the final size.
 If, as you're stuffing array elements, you run out of length, doubling the
 existing array length is a good strategy.
Mar 27 2002
parent reply "OddesE" <OddesE_XYZ hotmail.com> writes:
"Sean L. Palmer" <spalmer iname.com> wrote in message
news:a7s781$23gk$1 digitaldaemon.com...
 Why not just double the darray's allocated space when array.length++
exceeds
 the allocated space?  Oh, because you're only storing the length, assuming
 the allocated size == the length, always.

 Think of it like a std::vector<>::push_back().  Yeah it's inefficient but
 sometimes it's the most elegant way to code an algorithm, and not
everybody
 is always primarily concerned with speed.  Think college D courses.  ;)

 Sean
Delphi TList uses a Capacity property. Count is the exact number of elements in the list, Capacity is the amount of allocated elements. Capacity - Count = WastedSpace :) -- Stijn OddesE_XYZ hotmail.com http://OddesE.cjb.net __________________________________________ Remove _XYZ from my address when replying by mail
Mar 27 2002
parent "OddesE" <OddesE_XYZ hotmail.com> writes:
"OddesE" <OddesE_XYZ hotmail.com> wrote in message
news:a7ti96$2qni$1 digitaldaemon.com...
 Capacity - Count = WastedSpace  :)
Delphi typo... :) Should be: Capacity - Count == WastedSpace
Mar 27 2002