Jump to page: 1 2 3
Thread overview
String Comparison and Dynamic Arrays?
Mar 26, 2002
Gary
Mar 26, 2002
Pavel Minayev
Mar 26, 2002
Gary
Mar 27, 2002
Walter
Mar 27, 2002
Gary
Mar 27, 2002
Walter
Mar 27, 2002
Gary
Mar 27, 2002
Walter
Mar 27, 2002
Russ Lewis
Mar 27, 2002
Russ Lewis
Mar 27, 2002
OddesE
Mar 27, 2002
Gary
Mar 27, 2002
Pavel Minayev
Mar 27, 2002
Walter
Mar 27, 2002
Pavel Minayev
Mar 29, 2002
Walter
Mar 29, 2002
Pavel Minayev
Mar 29, 2002
Walter
Mar 30, 2002
Pavel Minayev
Apr 07, 2002
Walter
Apr 07, 2002
Pavel Minayev
Apr 08, 2002
Walter
Apr 08, 2002
Pavel Minayev
Apr 12, 2002
Walter
Mar 27, 2002
Sean L. Palmer
Mar 27, 2002
OddesE
Mar 27, 2002
OddesE
March 26, 2002
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.


March 26, 2002
"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 =)


March 26, 2002
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 =)
>
>


March 27, 2002
"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.


March 27, 2002
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?


March 27, 2002
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.
>
>


March 27, 2002
"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.



March 27, 2002
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.


March 27, 2002
"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>.


March 27, 2002
"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--;

?


« First   ‹ Prev
1 2 3