RETURN TO INDEX

BASIC LESSON 7 - GROUP VII functions

MyLittleBase 2.0 API is divided into 7 groups of functions

1- Initialization functions
2-Fields functions
3-Rows functions
4-Navigation functions
5-Data management functions
6-File management functions
7-Utils

This lesson deals with the seventh group (utils functions)
and all what you can do with them.

This seventh group contains functions

RobustStrToFloat (7.1)
RobustFloatToStr (7.1)
SortByData (7.2)
SortByFloat (7.2)
RandomSort(7.2)
Distinct (7.3)
MakeDistinct (7.3)
AreSameRows (7.3)
Fusion (7.4)

 

RobustStrToFloat and RobustFloatToStr are the same as StrToFloat and FloatToStr delphi functions, but they are intended never to fail.
DELPHI C++
To convert a string to a float or integer value:
RobustStrToFloat:
{all these samples are correct}

var v: real;
     k: integer;
v := myinstance.RobustStrToFloat('3.16');

v := myinstance.RobustStrToFloat('3,16');
k := trunc(myinstance.RobustStrToFloat('3'));
{while StrToFloat fails with some decimal separators (, or .), RobustStrToFloat never fails and is able to read any string float format}

To convert a float to a string:
RobustFloatToStr:
Caption := myinstance.RobustFloatToStr(3.16);

To convert a string to a float or integer value:
RobustStrToFloat:
/*all these samples are correct*/

float v;
int k;
v = myinstance->RobustStrToFloat("14.2");

v = myinstance->RobustStrToFloat("14,2");
k = (int)myinstance.RobustStrToFloat("4");

To convert a float to a string:
RobustFloatToStr:
printf("%s", myinstance->RobustFloatToStr(3.16));

 

SortByData, SortByFloat, RandomSort are used to sort rows.
DELPHI C++
To Sort rows with a STRING field's values:
use the SortByData API:
For sample a table has 2 fields, Name and Address, and you want to sort the table according to Name values:

var b: boolean;
b := myinstance.SortByData('Name', MLB_LOWEST);
or
b := myinstance.SortByData('Name', MLB_GREATEST);

To Sort rows with a FLOAT field's values:
use the SortByFloat API:
For sample a table has 2 fields, Name and ZipCode, and you want to sort the table according to ZipCode values as integer values (not as string values):

var b: boolean;
b := myinstance.SortByFloat('ZipCode', MLB_LOWEST);
or
b := myinstance.SortByFloat('ZipCode', MLB_GREATEST);

const MLB_LOWEST = false;
const MLB_GREATEST = true;

MLB_LOWEST sorts the table from the lowest value to the greatest.
MLB_GREATEST sorts the table from the greatest value to the lowest.

All these functions return true if successful, false else.

To Randomly Sort rows:
myinstance.RandomSort;
{No return value, it is a procedure}

To Sort rows with a STRING field's values:
use the SortByData API:
For sample a table has 2 fields, Name and Address, and you want to sort the table according to Name values:

bool b;
b = myinstance->SortByData("Name", MLB_LOWEST);
or
b = myinstance->SortByData("Name", MLB_GREATEST);

To Sort rows with a FLOAT field's values:
use the SortByFloat API:
For sample a table has 3 fields, Name, Address and Income, and you want to sort the table according to Income values as float values (not as string values):

bool b;
b = myinstance->SortByFloat("Income", MLB_LOWEST);
or
b = myinstance->SortByFloat("Income", MLB_GREATEST);

#define MLB_GREATEST true
#define MLB_LOWEST false

MLB_LOWEST sorts the table from the lowest value to the greatest.
MLB_GREATEST sorts the table from the greatest value to the lowest.

All these functions return true if the loading is successful, false else.

To Randomly Sort rows:
myinstance->RandomSort();
/*no return value*/


Distinct, MakeDistinct, AreSameRows functions are used to be sure that no row are duplicated (duplicated means that 2 rows have exactly the same record's value for each field).
DELPHI C++
To make all rows distincts:
use the MakeDistinct API:
The Distinct boolean property is only true when the table's rows are the all distincts and no row is duplicated.
The MakeDistinct function deletes any duplicated row, and makes the table distinct.
It is a procedure, no return value.

Sample:
myinstance.MakeDistinct;
{now Distinct is true}

To compare 2 rows
:
var b: boolean;
b := myinstance.AreSameRows(4, 6);
{compares rows 4 and 6, if the rows are the same (duplication), it returns true, else it returns false}
To make all rows distincts:
use the MakeDistinct API:
The Distinct boolean property is only true when the table's rows are the all distincts and no row is duplicated.
The MakeDistinct function deletes any duplicated row, and makes the table distinct.
It is a procedure, no return value.

Sample:
myinstance->MakeDistinct();
/*now Distinct is true*/


MakeDistinct method and Distinct property, were created to prepare a minimal sql engine, to query the table, and to answer to the sql request SELECT DISTINCT.

To compare 2 rows
:
bool b;
b = myinstance->AreSameRows(1, 2);
/*compares rows 1 and 2, if the rows are the same (same values for each field), it returns true, else it returns false*/

 

Fusion function is used to paste one table rows to another, mixing fields if tables have not the same structure (fields of the resulting table can be chosen from any of the 2 source tables fields).
DELPHI C++
To add one table's row to another table:
use the Fusion API:
The Fusion API uses as parameter 3, a special type named TMlbFusionArray.
This type is simply a string formatted like that:
1- first the string begins with the words ALL or COMMON.
ALL means that all fields of the 2 tables on which Fusion is applied will be used in the resulting table. (resulting table's fields will be the current table's fields + the table to fusion's fields).
COMMON means that only fields appearing both in the source tables to fusion will be used in the resulting table.

2- Then ALL or COMMON are followed by the CSVSeparator char.

3- Then fields' names having to appear in the resulting table are listed and separated by CSVSeparator chars.

Samples:
{myinstance is a table made of 2 fields, name and zipcode}

{source is a table made of 3 fields, name, age and income}

{to fusion, myinstance table, with source table, and to put the result in the mytarget table}
var b: boolean;

b := myinstance.Fusion(source, mytarget, 'ALL;*');
{target is now a table made of 4 fields, name, zipcode, age and income, and contains rows of both myinstance and source}

b := myinstance.Fusion(source, mytarget, 'ALL;name;age');
{target is now a table made of 2 fields, name and age, and contains rows of both myinstance and source, for these 2 fields}

b := myinstance.Fusion(source, mytarget, 'COMMON;*');
{target is now a table made of 1 field, name, because name is the only one field common to the myinstance table and the source table. Target contains names records of both myinstance and source}

To add one table's row to another table:
use the Fusion API:

Samples:
/*myinstance is a table made of 3 fields, name, firstname and zipcode*/

/*source is a table made of 4 fields, name, firstname, age and income*/

/*to fusion, myinstance table, with source table, and to put the result in the mytarget table*/
bool b;

myinstance->CSVSeparator = ",";
b = myinstance->Fusion(source, mytarget, "ALL,*");
/*mytarget is now a table made of 5 fields, name, firstname, zipcode, age and income, and contains both rows of myinstance and source*/

myinstance->CSVSeparator = "x";
b = myinstance.Fusion(source, mytarget, "ALLxnamexfirstname");
/*mytarget is now a table made of 2 fields, name and firstname, and contains rows of both myinstance and source, for these 2 fields*/

myinstance->CSVSeparator = ";";
b = myinstance->Fusion(source, mytarget, "COMMON;*");
/*mytarget is now a table made of the 2 common fields, name and firstname and contains names records of both myinstance and source*/
EXERCISE 7- Use group VII functions to initialize 2 tables with mlb files, to use Fusion on all fields of the 2 tables, to make the resulting table Distinct, and to Sort it by the Name string field, finally to save it to a new comma separated file.
DELPHI C++
unit Unit1;

interface

uses
SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls, Forms, Dialogs, mlb2;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);

procedure FormDestroy(Sender: TObject);
private

public
end;

var
Form1: TForm1;
mlb: TMlb2;
table2: TMlb2;
target: TMlb2;

implementation

{$R *.DFM}

procedure TForm1.FormCreate(Sender: TObject);
begin

    mlb := TMlb2.Create;
    table2 := TMlb2.Create;
    target := TMlb2.Create;
    mlb.LoadFromFile('source_table1.mlb');
    table2.LoadFromFile('source_table2.mlb');
    mlb.CSVSeparator := '+';
   mlb.Fusion(table2, target, 'ALL+*');
   target.MakeDistinct;
   if target.SortByData('Name') then begin

     Caption := 'target sorted';
   end else begin

   end;
    target.SaveToFile('target_table.csv');

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

    target.Free;
    table2.free;
    mlb.Free;
end;

end.

#include <stdio.h>;
#include "mlb2.h";

int main(int argc, char* argv[]) {
    TMlb2* mlb = mlb->Create();
    TMlb2* source2 = source2->Create();
    TMlb2* target = target->Create();

    mlb->LoadFromFile("source_table1.mlb");
    table2->LoadFromFile("source_table2.mlb");
    mlb->CSVSeparator = ':';
   mlb->Fusion(table2, target, "ALL:*");
   target->MakeDistinct();
   if (target->SortByData("Name")) {

   } else {
   }
    target->SaveToFile("target_table.csv");


    target->Destroy();
    source2->Destroy();
    mlb->Destroy();
}
GOTO INDEX>>
© ANIROM Multimedia