Author Topic: function CheckDigit(const Input: string): boolean;  (Read 2099 times)

0 Members and 1 Guest are viewing this topic.

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
function CheckDigit(const Input: string): boolean;
« on: July 21, 2008, 06:44:06 pm »
Script Name: CheckDigit
Script Description: CheckDigit() checks an Input for integer literals.
Original Author(s): Dual

Hosted by: http://soldatcentral.com/
Core Version:  2.6.3
Compile Test: Passed

Code: [Select]
function CheckDigit(const Input: string): boolean;
var
  i, len: integer;
  Ch: char;
begin
  len:= length(Input);
  for i:= 1 to len do
  begin
    Ch:= Input[i];
    if ((Ord(Ch) - 48 >= 0) and (Ord(Ch) - 48 <= 9)) then
    begin
      Result:= true;
      continue;
    end else
    begin
      if ((Ch = ' ') and (not(Result))) then
      begin
        continue;
      end else
      begin
        Result:= false;
        break;
      end;
    end;
  end;
end;

Parameters

Input: string
String to be checked (e.g. user input).

Return Value

The return value depends on the Input. On success CheckDigit() returns true and validates the string for converting it with StrToInt().

Note

CheckDigit() will also return true if you pass a string like '  5' because the input can be converted by StrToInt().

Update(s)

29/30-Oct-2008:
  • minor code improvements

3-Aug-2008:
  • added support for spaces


« Last Edit: October 31, 2008, 07:11:04 am by Markus Quär »
Soldat Global Account System: #soldat.sgas @ quakenet

Offline danmer

  • Camper
  • ***
  • Posts: 466
  • crabhead
Re: CheckDigit (in relation of user input)
« Reply #1 on: July 22, 2008, 09:35:44 am »
nice... And i suppose it's faster than RegExpMatch('^[0-9]*$',input) ?

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: CheckDigit (in relation of user input)
« Reply #2 on: July 22, 2008, 11:08:41 am »
Maybe it works faster :)
« Last Edit: October 30, 2008, 02:12:14 pm by Markus Quär »
Soldat Global Account System: #soldat.sgas @ quakenet

Offline DorkeyDear

  • Veteran
  • *****
  • Posts: 1507
  • I also go by Curt or menturi
Re: CheckDigit (in relation of user input)
« Reply #3 on: July 22, 2008, 05:47:26 pm »
http://forums.soldat.pl/index.php?topic=27844 -- a method of timing it, fairly simple (although not always correct after an hour of waiting i think, if i remember correctly, but im sure your not testing for over an hour)

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: CheckDigit (in relation of user input)
« Reply #4 on: July 22, 2008, 06:25:35 pm »
Yea, your idea is great! :)
I will try it out some time.
« Last Edit: October 30, 2008, 02:12:32 pm by Markus Quär »
Soldat Global Account System: #soldat.sgas @ quakenet

Offline rhide

  • Major
  • *
  • Posts: 60
  • Coffee-addict
    • Vrastar-Hai soldat clan
Re: CheckDigit (in relation of user input)
« Reply #5 on: July 22, 2008, 06:46:04 pm »
To be honest are loops generally pretty slow, often slower than built in functions such as regexp. Also, you only test for 1-9, commas ans periods are also most often considered numeric. Dont forget spaces btw
Warning! Division by zero seriously injures yourself and the people in your surroundings.

Proud member of Vrastar-Hai:
http://www.guldheden.com/~erga050/vrastar-hai

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: CheckDigit (in relation of user input)
« Reply #6 on: July 22, 2008, 07:12:06 pm »
To be honest are loops generally pretty slow, often slower than built in functions such as regexp. Also, you only test for 1-9, commas ans periods are also most often considered numeric. Dont forget spaces btw

I'm wondering whether RegExpMatch uses loops too, but I'm almost sure ...  it makes use of them!?
To be honest I'm checking for 0-9 - try it out yourself , it works ;)
Adding support for commas, spaces and periods are great suggestions for upcoming versions. I will think about it.
Soldat Global Account System: #soldat.sgas @ quakenet

Offline SpiltCoffee

  • Veteran
  • *****
  • Posts: 1579
  • Spilt, not Split!
    • SpiltCoffee's Site
Re: CheckDigit (in relation to user input)
« Reply #7 on: August 08, 2008, 11:46:49 pm »
RegExpMatch('^\d+$',Source) (or something along those lines) would work faster, IMO, as you'd only do one check opposed to a whole for loop for each character with your current method.
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: CheckDigit (in relation to user input)
« Reply #8 on: August 09, 2008, 06:58:57 am »
During the development of CheckDigit I had never pursued the aim to create a function which works faster than RegExpMatch. I only try to offer people who are interested in soldat scripting another possibility to check a string for numeric characters. This script is supposed to address particularly people who aren't able to use RegExpMatch  ;)
Soldat Global Account System: #soldat.sgas @ quakenet

Offline CurryWurst

  • Camper
  • ***
  • Posts: 265
    • Soldat Global Account System
Re: function CheckDigit(const Input: string): boolean;
« Reply #9 on: October 30, 2008, 02:04:39 pm »
UPDATE
  • minor code improvements
Soldat Global Account System: #soldat.sgas @ quakenet