Author Topic: (Integer-) Comparison Function buggy!?  (Read 1718 times)

0 Members and 1 Guest are viewing this topic.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
(Integer-) Comparison Function buggy!?
« on: February 05, 2009, 01:02:42 pm »
Does anyone ever attempt to compare two strings represting integer values?

Code: [Select]
procedure ActivateServer();
var
  Str1, Str2: string;
begin
  Str1:= '28950';
  Str2:= '500';
  if (Str1 > Str2) then
  begin
    WriteLn(Str1 + ' is greater than ' + Str2);
  end else
  begin
     WriteLn(Str2 + ' is greater than ' + Str1 + ', really??');
  end;
end;

.... the result is dubious!!

C++ examples like this work well. I don't know how the Soldat scripting engine compares strings,
but this instance raises doubts about it.
Soldat Global Account System: #soldat.sgas @ quakenet

Offline EnEsCe

  • Retired Soldat Developer
  • Flamebow Warrior
  • ******
  • Posts: 3101
  • http://enesce.com/
    • [eC] Official Website
Re: (Integer-) Comparison Function buggy!?
« Reply #1 on: February 05, 2009, 05:14:55 pm »
Its not a bug, it was never intended for strings to be compared like that. It's a terrible way to code and unoptimal. Pascal is not C++.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #2 on: February 05, 2009, 05:58:50 pm »
Sure, Pascal is not C++ but something like this could be really useful especially if you try to implement sort algorithms.
Owing to this problem my QuickSort procedures are divided into two seperate ones.

I thought about filling the compare string which is less in length with zeros, so both strings are equal in length.
Unfortunately this did not work out.

It's up to you :P
Soldat Global Account System: #soldat.sgas @ quakenet

Offline iDante

  • Veteran
  • *****
  • Posts: 1967
Re: (Integer-) Comparison Function buggy!?
« Reply #3 on: February 05, 2009, 06:46:06 pm »
strtoint...

Or you could write a quick thing to compare them based on the actual chars in the string (would be recursive but easy to do).
« Last Edit: February 06, 2009, 06:20:33 am by iDante »

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #4 on: February 05, 2009, 07:09:23 pm »
strtoint...
Owing to this problem my QuickSort procedures are divided into two seperate ones.

*One is based on StrToInt, the other on just on string compare.

Or you could write a quick thing to compare them based on the actual chars in the string (would be reflexive but easy to do).

Nice idea, I may pursue it :D
Soldat Global Account System: #soldat.sgas @ quakenet

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #5 on: February 06, 2009, 06:53:38 am »
I thought about filling the compare string which is less in length with zeros, so both strings are equal in length.
o.O Sorry, I have to redefine my statement ...

Code: [Select]
function GetLongestString(var Str: array of string): integer;
var
  i: integer;
begin
   for i:= 0 to ArrayHigh(Str) do
   begin
     if (length(Str[i]) > length(Str[Result])) then
     begin
       Result:= i;
     end;
   end;
end;

function FillWith(const Filler: char; Amount: integer): string;
var
  i: integer;
begin
  for i:= 1 to Amount do
  begin
    Result:= Filler + Result;
  end;
end;

procedure SelectionSort(var Field: array of string; Left, Right: integer);
var
  Buffer: string;
  i, j, m: integer;
begin
  i:= 1;
  m:= GetLongestString(Field);
  while (j < Right) do
  begin
    Field[j]:= FillWith('0', m - length(Field[j]));
    Field[j + i]:= FillWith('0', m - length(Field[j + i]));
    if (Field[j] > Field[j + i]) then
    begin
      Buffer:= Field[j];
      Field[j]:= Field[j + i];
      Field[j + i]:= Buffer;
    end;
    if (j + i < Right) then
    begin
      Inc(i, 1);
    end else
    begin
      i:= 1;
      Inc(j, 1);
    end;
  end;
end;

It's working but it's not supposed to because there are two lines of weird code:

Field[j]:= FillWith('0', m - length(Field[j]));
Field[j + i]:= FillWith('0', m - length(Field[j + i]));

After calling FillWith() two times both string must contain zeros because I did not add their old content Field[j] and Field[j + i] to them,
but they contain zeros + old content ??? ???

Any explanations?
Soldat Global Account System: #soldat.sgas @ quakenet

Offline SpiltCoffee

  • Veteran
  • *****
  • Posts: 1579
  • Spilt, not Split!
    • SpiltCoffee's Site
Re: (Integer-) Comparison Function buggy!?
« Reply #6 on: February 06, 2009, 09:59:23 am »
If you remove 'm -', does the whole variable get filled?
When life hands you High Fructose Corn Syrup, Citric Acid, Ascorbic Acid, Maltodextrin, Sodium Acid Pyrophosphate,
Magnesium Oxide, Calcium Fumarate, Yellow 5, Tocopherol and Less Than 2% Natural Flavour... make Lemonade!

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #7 on: February 06, 2009, 10:34:20 am »
It gets filled to the amount of the variable length * 2. Assuming that there is string containing "225" as value.
Once the function FillWith() is called it contains "000225".
Soldat Global Account System: #soldat.sgas @ quakenet

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: (Integer-) Comparison Function buggy!?
« Reply #8 on: February 06, 2009, 10:42:23 am »
if Field[] is a tstringarray, you gotta setlength it to 0 and back if you want to reset the values. I had this bug with using xsplit - it kept the old values and added the ones i wanted to assign there to them...

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #9 on: February 06, 2009, 04:19:42 pm »
Hmm Field[] is string array and I did not use xsplit ???
Soldat Global Account System: #soldat.sgas @ quakenet

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #10 on: February 12, 2009, 05:29:59 pm »
Seriously, EnEsCe how can this script work?
It contains more than only one bug ....

Script
Code: [Select]
function GetMaxStringSize(const Field1: array of string): integer;
var
  i: integer;
begin
   for i:= 0 to ArrayHigh(Field1) do
   begin
     if (length(Field1[i]) > Result) then
     begin
       Result:= length(Field1[i]);
     end;
   end;
end;

function FillWith(const Filler: char; Amount: integer): string;
var
  i: integer;
begin
  for i:= 1 to Amount do
  begin
    Result:= Filler + Result;
  end;
end;

procedure SelectionSort(const Bunch: array of string; Left, Right: integer);
var
  Buffer: string;
  i, j, m: integer;
begin
  i:= 1;
  m:= GetMaxStringSize(Bunch);
  while (j < Right) do
  begin
    Bunch[j]:= FillWith('0', m - length(Bunch[j]));
    Bunch[j + i]:= FillWith('0', m - length(Bunch[j + i]));
    if (Bunch[j] > Bunch[j + i]) then
    begin
      Buffer:= Bunch[j];
      Bunch[j]:= Bunch[j + i];
      Bunch[j + i]:= Buffer;
    end;
    if (j + i < Right) then
    begin
      Inc(i, 1);
    end else
    begin
      i:= 1;
      Inc(j, 1);
    end;
  end;
end;

function IXSplit(const SOURCE: string; Delimiter: string): array of string;
var
  i, x, d: integer;
  s, b: string;
begin
  d:= length(Delimiter);
  i:= 1;
  SetArrayLength(Result, 0);
  while (i <= length(SOURCE)) do
  begin
    s:= Copy(SOURCE, i, d);
    if (s = Delimiter) then
    begin
      SetArrayLength(Result, x + 1);
      Result[x]:= b;
      Inc(i, d);
      Inc(x, 1);
      b:= '';
    end else
    begin       
      b:= b + Copy(s, 1, 1);
      Inc(i, 1);
    end;
  end;
  if (b <> '') then
  begin
    SetArrayLength(Result, x + 1);
    Result[x]:= b;
  end;
end;

procedure ActivateServer();
var
 i: integer;
 a: array of string;
begin
  a:= IXSplit(ReadFile('compare.txt'), #13#10);
  SelectionSort(a, 0, GetArrayLength(a) - 1);
  for i:= 0 to ArrayHigh(a) do
  begin
    WriteLn(a[i]);
  end;
end;

Compare.txt
Code: [Select]
200
4400
30
0
1
11
-10
-2
99000
4555
2
412987231
5621

I guess my PC has got a virus :o
Soldat Global Account System: #soldat.sgas @ quakenet

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: (Integer-) Comparison Function buggy!?
« Reply #11 on: February 19, 2009, 04:54:28 pm »
It seems that nobody tried to deal with my problem.
So, I decided to develop my own working function (Click)

It even sorts an array of string representing integer values ;)
Soldat Global Account System: #soldat.sgas @ quakenet