Ben's Random Codes lazarus / 8-Other ---------------------------------------------------------------------- BenJones · 2013-06-15 07:35 Hi here is were I am going to post some random codes that I make while learning Lazarus more that may help others. RLE (Run Length Encoding Example) This is two functions that allow you to compress a string of repeating letters. It works buy looking at the letters and counting them up and marking them with a number for example the string aaaaaaaapkkkk can be compressed to 8a1p4k the decompresser looks for the numbers then appending the number of letters needed. Here are functions. Code: [Select] function IsNumber(Source: string): boolean; var   i, flag: integer; begin   //Return true if number found.   Val(Source, i, flag);   if (flag = 0) then   begin     Result := True;   end   else   begin     Result := False;   end; end; function RLEDecode(Source: string): string; var   Temp, Buffer, ch: string;   x, y, cCount: integer; begin   //Uncompress RLE encoded string.   cCount := 0;   for x := 1 to Length(Source) do   begin     //Get current char     ch := Source[x];     //Check if we have come across a number.     if IsNumber(ch) then     begin       //Build number.       Temp := Temp + ch;     end     else     begin       //Convert temp to number.       cCount := StrToInt(Temp);       for y := 1 to cCount do       begin         //Build string         Buffer := Buffer + ch;       end;       Temp := '';     end;   end;   //Return result.   Result := buffer; end; function RLEEncode(Source: string): string; var   ch, buffer: string;   x, cCount: integer; begin   //Compress string to RLE encoded string.   cCount := 0;   ch := Source[1];   for x := 1 to Length(Source) do   begin     //Check for char match.     if ch = Source[x] then     begin       //Inc counter.       Inc(cCount);     end     else     begin       //Build string;       buffer := buffer + IntToStr(cCount) + ch;       cCount := 1;       ch := Source[x];     end;   end;   //Build string;   buffer := buffer + IntToStr(cCount) + ch;   //Return string.   Result := buffer; end;  Example Code: [Select]var   Test: string; begin   //Test RLE compression and decompression.   Test := RLEEncode('AAAAAAHLLLLLLLLaaaaaaaaaaaab');   ShowMessage('Compressed : ' + Test);   //Decompress   Test := RLEDecode(Test);   ShowMessage('Decompressed :' + Test); Bubble Sort Here is a small function to allow you to sort an array of random numbers. Code: [Select]procedure BSort(out Items: array of integer); var   x, Item: integer;   Sorted: boolean; begin   //Init variables.   Item := 0;   x := 0;   Sorted := False;   //While not sorted sort items.   while not Sorted do   begin     Sorted := True;     for x := Low(Items) to High(Items) do     begin       if Items[x] > Items[x + 1] then       begin         Sorted := False;         //Swap items         Item := Items[x];         Items[x] := Items[x + 1];         Items[x + 1] := Item;       end;     end;   end; end; Example Code: [Select]var   s: array[0..5] of integer;   x: integer; begin   //Some random integers.   s[0] := 4;   s[1] := 2;   s[2] := 0;   s[3] := 3;   s[4] := 5;   s[5] := 1;   //Bubble Sort Items.   BSort(s);   for x := 0 to High(s) do   begin     //Display Items.     ShowMessage(IntToStr(s[x]));   end ---------------------------------------------------------------------- eny · 2013-06-15 08:05 http://mashable.com/2007/08/06/free-blog-hosts/ ---------------------------------------------------------------------- BenJones · 2013-06-15 08:31 Thanks for the blog links I have a look I never used blog suff before. I may try and see if I can find some free webspace maybe about 10MB cos I just want somewere to post my codes. I was thinking of an idea of makeing some kind of offline client, were people can update it with new sniplets from the and view all the codes of line, There used to be a program years ago I liked for QBasic ---------------------------------------------------------------------- eny · 2013-06-15 19:35 As much fun as it is to see some1 embark on a journey to learn FPC/Lazarus, the intermediate results of those 'experiments' are not interesting. Especially if the results are custom versions of functionality that is already available out of the box (just more buggy). Hence my suggestiong to post the results of your experiments on your own blog. Then people who really wish to see how you progress can read those posts. See also the suggestions in the second post you made: http://www.lazarus.freepascal.org/index.php?topic=14317.0;topicseen If you get stuck or have a question of some sort, do not hesitate to ask.