Recent

Author Topic: Lookup-tables article  (Read 7026 times)


eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Lookup-tables article
« Reply #1 on: February 02, 2011, 01:27:05 pm »
Interesting topic; not the best of examples though.
And Lazarus already has out of the box support for hashes (and so has Delphi IIRC).
So no need to reinvent the wheel (except when you want to make a better wheel  8)).
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

qdos

  • Full Member
  • ***
  • Posts: 112
Re: Lookup-tables article
« Reply #2 on: February 02, 2011, 02:46:14 pm »
Interesting topic; not the best of examples though.
And Lazarus already has out of the box support for hashes (and so has Delphi IIRC).
So no need to reinvent the wheel (except when you want to make a better wheel  8)).

Yeah i know. I got two kids running around the house so i didnt have time for any better :)
Added a more practical example:

When working with filestreams you usually end up with either opening up an existing file, or creating a new one. In this particular case it means that there are only two possible outcomes. Which just happens to be the behavior of a boolean value. So we can save us some code by taking advantage of this in a minimalistic lookup array:

Procedure TForm1.OpenFile(filename:String);
const
  CLookup:array[boolean] of word = (fmCreate,fmOpenRead);
var
  mFile:  TFileStream;
Begin
  try
    mFile:=TFileStream.Create(filename,CLookup[fileexists(filename)]);
    try
      //Use stream here
    finally
      mfile.free;
    end;
  except
    on e: exception do
    Raise exception.CreateFmt('Failed to access file <%s>, %s',[e.message]);
  end;
end;
« Last Edit: February 02, 2011, 03:04:58 pm by qdos »

eny

  • Hero Member
  • *****
  • Posts: 1634
Re: Lookup-tables article
« Reply #3 on: February 02, 2011, 02:54:47 pm »
I know. I tried to make it better but i got 2 kids running around here :)

Tie them to a chair in front of a screen with the Lazarus wiki about threads.
That'll teach'm  :D
All posts based on: Win10 (Win64); Lazarus 2.0.10 'stable' (x64) unless specified otherwise...

qdos

  • Full Member
  • ***
  • Posts: 112
Re: Lookup-tables article
« Reply #4 on: February 02, 2011, 02:58:09 pm »
I know. I tried to make it better but i got 2 kids running around here :)

Tie them to a chair in front of a screen with the Lazarus wiki about threads.
That'll teach'm  :D

He he :) Thank god for Mickey's playhouse !

 

TinyPortal © 2005-2018