Solved

Quick String "Ticker"?

Posted on 2002-07-19
4
143 Views
Last Modified: 2010-04-04
Does anyone have any good ideas for a quick and easy routine, to "tick" through all combinations of a-z and 0-9 characters?

Rather like a stop-watch, but with characters.

Example :

aaaaaa
aaaaab
aaaaac
aaaaad
......
......
aaaaba
aaaabb
......
......
......
aaa9ca
aaa9cb
aaa9cc
......
etc. etc. etc.

Thanks in advance.
0
Comment
Question by:nicey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 26

Accepted Solution

by:
Russell Libby earned 150 total points
ID: 7165760

An object oriented (and very fast) approach....

Rollover capability (when number is maxed "999..." etc) and works from right to left. Works by incrementing from a-z0-9, when 9 move left until not 9. If at null, then reset to all 'a', else increment and go left to right setting back to 'a'.
 
Hope this fits your needs,
Russell

---------------------------------

unit ticker;

interface

uses
  Windows, SysUtils;

type
  ETickClass     =  class(Exception);
  TTickClass     =  class(TObject)
  private
     // Private declaration
     FSize:      Integer;
     FBuffer:    PChar;
     FStart:     PChar;
     FHead:      PChar;
     FTail:      PChar;
  protected
     // Protected declaration
     function    GetValue: String;
  public
     // Public declaration
     constructor Create(Size: Integer);
     destructor  Destroy; override;
     property    Value: String read GetValue;
     property    Size: Integer read FSize;
     procedure   Tick;
  end;

implementation

constructor TTickClass.Create(Size: Integer);
begin

  // Perform inherited
  inherited Create;

  // Set values
  FBuffer:=nil;
  FSize:=Size;

  // Check size
  if (FSize <= 0) then raise ETickClass.Create('Size must be greater than zero');

  // Allocate buffer
  FBuffer:=AllocMem(Size+2);

  // Set up buffers and pointers
  FHead:=FBuffer+1;
  FillMemory(FHead, Size, Byte('a'));
  FTail:=StrEnd(FHead);
  FStart:=FTail;
  Dec(FStart);

end;

destructor TTickClass.Destroy;
begin

  // Free memory
  if Assigned(FBuffer) then FreeMem(FBuffer);

  // Perform inherited
  inherited Destroy;

end;

function TTickClass.GetValue;
begin

  // Return current buffer value
  result:=FHead;

end;

procedure TTickClass.Tick;
var  ppos:       PChar;
begin

  // Set starting position
  ppos:=FStart;

  // Handle current position
  case ppos^ of
     // Jump to numerics
     'z'   :  ppos^:='0';
     // Nine is where the real work comes in
     '9'   :
     begin
        // Loop all the way to the front while nines and not null
        while (ppos^ = '9') do Dec(ppos);
        // If the whole string is nines then reset all to a's and were done
        if (ppos^ = #0) then
           FillMemory(FHead, FSize, Byte('a'))
        else
        begin
           // We are not at null, so increment byte
           if (ppos^ = 'z') then
              ppos^:='0'
           else
              Inc(Byte(ppos^));
           // Walk back from left to right
           Inc(ppos);
           while (ppos^ <> #0) do
           begin
              ppos^:='a';
              Inc(ppos);
           end;
        end;
     end;
  else
     Inc(Byte(ppos^));
  end;

end;

0
 
LVL 1

Author Comment

by:nicey
ID: 7165825
Mate, you're a star!

It works very nice indeed.  I've hooked it into my program, and the job is now a good one.

I'm just off to finish off the project.

Thanks again. ;-)
0
 
LVL 26

Expert Comment

by:Russell Libby
ID: 7165852

Anytime :-)

I also should have mentioned that the Size param on the create is used to define the length of your ticker string, but I think you already found that out.

Have a great weekend,
Russell
0
 
LVL 1

Author Comment

by:nicey
ID: 7165896
Yeah, the code is pretty tight - it only took a few read throughs, and it all sunk in.

Only thing is, my 500Mhz AMD chip is really "running" when working with the larger patterns. :-D

S'ok though, as the final program will be run a much much more upto date system.

Good weekend to you too. ;-)
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

739 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question