Comparing cell content against a variable in free pascal - freepascal

is there a way to compare the content of a cell in a grid against the content of a variable.
I need to compare cell 0,1 and if it's value is lower (or higher) than variable x then something happens.
I'm using Lazarus and a StringGrid.

You can try something like this (for integer values, but can easily be modified for another ordinal types). You can follow the commented version of this post as well:
uses
Math;
function CompareValueEx(StringGrid: TStringGrid; const Column, Row: Integer;
const Value: Integer; out Relationship: TValueRelationship): Boolean;
var
Output: Integer;
begin
Result := TryStrToInt(StringGrid.Cells[Column, Row], Output);
if Result then
Relationship := CompareValue(Value, Output);
end;
procedure TForm1.Button1Click(Sender: TObject);
var
I: Integer;
Relationship: TValueRelationship;
begin
I := 111;
StringGrid1.Cells[1, 2] := '112';
if CompareValueEx(StringGrid1, 1, 2, I, Relationship) then
begin
case Relationship of
EqualsValue: ShowMessage('The values are the same');
LessThanValue: ShowMessage('The I value is less than in cell [1;2]');
GreaterThanValue: ShowMessage('The I value is greater than in cell [1;2]');
end;
end
else
ShowMessage('The value in cell [1;2] is not a valid integer value!');
end;

Related

delphi mysql query result

for read data from database server im using mysql query like this:
FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
but when i have to use a loop for read data from mysql
i need a array to read and store data on it
i know in php its like this
$arr = array();
while($obj = $q->fetch_object() ){
$arr[] = $obj;
}
but how can i do this syntax in Delphi ?
You don't need to do FDQuery1.First as that is implicit in the FDQuery1.Open. However if you want to know how many records you have it's important to do a FDQuery1.Last; before using FDQuery1.RecordCount to get the true record count otherwise you might get strange results.
All you need to do get your data into a memo is this
FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
FDQuery1.Open;
While not FDQuery1.Eof do
begin
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
FDQuery1.next;
end;
end;
FDQuery1.Close;
although a better solution with minimal exception handling is
FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
memo1.Lines.clear;
try
try
begin
FDQuery1.Open;
While not FDQuery1.Eof do
begin
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
FDQuery1.next;
end;
end;
end;
except
on E : Exception do
begin
showmessage ('Exception class name = '+E.ClassName+ slinebreak
+ 'Exception message = '+E.Message);
end //on E
end; //try-except
finally
FDQuery1.Close;
end; //try-finally
You mention an array. This is not needed if you only want to put the data in a memo but if you did want to put the data in an array (a dynamic array of variants as you don't know at design time how many record elements you need, how many field elements you need or what type each field is) then you would use the following code.
(Note this is deliberately not optimised code as I was trying to make the process clear)
Const
FirstRecordIndex = 0;
FirstFieldIndex = 0;
Var
DataArray : Variant;
TheRecordCount, TheFieldCount,
RecordNumber, FieldNumber : integer;
Data : variant;
begin
FDQuery1.SQL.Text := 'select * from table WHERE dcid=3;';
FDQuery1.Open;
FDQuery1.Last; //to get correct recordcount
TheRecordCount := FDQuery1.RecordCount;
TheFieldCount := FDQuery1.FieldCount;
FDQuery1.First; //go back to the beginning of the dataset
//set the dimensions of the 2D array of variants to hold data
DataArray := VarArrayCreate([FirstRecordIndex, TheRecordCount, FirstFieldIndex, TheFieldCount], varVariant ); //element can be of any type
//fill it
RecordNumber := -1; //initialise record indexe to just before the start
While not FDQuery1.Eof do
begin
inc(RecordNumber); //point to next record element in the array
for FieldNumber := FirstFieldIndex to TheFieldCount -1 do //load all the fields of this record
begin
Data := FDQuery1.Fields[FieldNumber].asVariant; //get the data
DataArray[RecordNumber, FieldNumber] := Data; //put into array
end;
FDQuery1.next; //get next record
end; //while
end;
FDQuery1.Close;
end;
To get the data back again use
For RecordNumber := FirstRecordIndex to TheRecordCount -1 do
For FieldNumber := FirstFieldIndex to TheFieldCount -1 do
begin
Data := DataArray[RecordNumber, FieldNumber] ;
//do something with the data ie put into a memo
end;
You can use this, i hope that help u:
FDQuery1.SQL.Text := 'select * from `table` WHERE dcid=3;';
FDQuery1.Open;
if FDQuery1.Active and (FDQuery1.RecordCount > 0) then
begin
FDQuery1.First;
While not FDQuery1.Eof do
begin
memo1.Lines.Add( FDQuery1.FieldByName('value').AsString );
FDQuery1.next;
end;
end;
you can do this with array of array consept
for this first you must create your new type
type
myArray = array [1..10] of integer;
myData= array of myArray;
and then you must define your variable
var
Records: myData;
i:integer;
on,now we have aarray of array to save database records on it so
begin
FDQuery1.SQL.Text := 'select * from table WHERE type=3;';
FDQuery1.Open;
FDQuery1.First;
SetLength(Records ,FDQuery1.recordcount);//or maybe you need a do a select count(*) query first
i:=0;
While not FDQuery1.Eof do
begin
Records[i][1]:= FDQuery1.FieldByName('value1').AsString;
Records[i][2]:= FDQuery1.FieldByName('value2').AsString;
Records[i][3]:= FDQuery1.FieldByName('value3').AsString;
Records[i][4]:= FDQuery1.FieldByName('value4').AsString;
Records[i][5]:= FDQuery1.FieldByName('value5').AsString;
Records[i][6]:= FDQuery1.FieldByName('value6').AsString;
i:=i+1;
FDQuery1.next;
end;
FDQuery1.Close;
end;
now all of your selected database records are in your local variable
you can go over the row and make new queries with FDQuery1...

get the whole word string after finding the word in a text

i have a problem developing this function, i have this text..
Testing Function
ok
US.Cool
rwgehtrhjyw54 US_Cool
fhknehq is ryhetjuy6u24
gflekhtrhissfhejyw54i
my function :
function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
begin
Result := 0;
for i:= 1 to Length(sTheString) do
begin
x := Copy(sTheString,i,Length(sWordToFind));
if X = sWordToFind then
begin
if X.Length > sWordToFind.Length then
begin
Result := 100;
break;
end else
begin
Result := i;
break;
end;
end;
end;
end;
now, i want X to be US.Cool, but here its always = US, because i want to check the length of sWordToFind and X.
After clarification, this question is about getting length of a word searched by its starting substring within a string. For example when having string like this:
fhknehq is ryhetjuy6u24
When you execute a desired function for the above string with the following substrings, you should get results like:
hknehq → 0 → substring is not at the beginning of a word
fhknehq → 7 → length of the word because substring is at the beginning of a word
yhetjuy6u24 → 0 → substring is not at the beginning of a word
ryhetjuy6u24 → 12 → length of the word because substring is at the beginning of a word
If that is so, I would do this:
function GetFoundWordLength(const Text, Word: string): Integer;
const
Separators: TSysCharSet = [' '];
var
RetPos: PChar;
begin
Result := 0;
{ get the pointer to the char where the Word was found in Text }
RetPos := StrPos(PChar(Text), PChar(Word));
{ if the Word was found in Text, and it was at the beginning of Text, or the preceding
char is a defined word separator, we're at the beginning of the word; so let's count
this word's length by iterating chars till the end of Text or until we reach defined
separator }
if Assigned(RetPos) and ((RetPos = PChar(Text)) or CharInSet((RetPos - 1)^, Separators)) then
while not CharInSet(RetPos^, [#0] + Separators) do
begin
Inc(Result);
Inc(RetPos);
end;
end;
I spend a few times on your idea, so i wrote below codes, but it is not a good way for develop a Start With search. with some research you can find builtin functions, that provide better performance. you can try StrUtils.SearchBuf Function it will provide a full function string search.
anyway this code are working with SPACE separator, I hope it will be useful for you:
function TForm5.FindWordInString(sWordToFind, sTheString : String): Integer;
var
i : Integer; x:String;
flag : Boolean;
begin
Result := 0;
i := 1;
flag := False;
while True do
begin
if i > Length(sTheString) then Break;
if not flag then
x := Copy(sTheString,i,Length(sWordToFind))
else
begin
if sTheString[i] = ' ' then Break;
x := x + sTheString[i];
end;
if (X = sWordToFind) then
begin
flag := True;
if (X.Length >= sWordToFind.Length) and
(sTheString[i + Length(sWordToFind)] = ' ') then
break
else
i := i + Length(sWordToFind) -1;
end;
i := i + 1;
end;
Result := Length(x);
end;

Conditional Statement Not Working With Records

How do i display all customers who have purchased an item on a certain day, my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure. Sorry if this is a simple question, i'm still new to programming.
type
Tday = (monday, tuesday);
Tcustomer = record
itemPurchased:string;
dayPurchased: Tday;
end;
Tcustomers = array of Tcustomer;
function readDay(prompt:string): Tday;
var
selection:Integer;
begin
writeln('1. Monday');
writeln('2. Tuesday');
selection := ReadIntegerRange('Select day purcased (1 - 3): ', 1, 3);
result := Tday(selection-1);
end;
function readCustomers(prompt:string):TCustomers;
var
numOfCustomers:integer;
i:integer;
begin
numOfCustomers := ReadInteger('Enter number of customers: ');
setLength(result, numOfCustomers);
for i := 0 to high(result) do
begin
writeln(i);
result[i].itemPurchased := ReadString('Item Purchased: ');
result[i].dayPurchased := readDay(prompt);
end;
end;
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
begin
specific_day := ReadInteger('Enter day to see items purchased');
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
end;
procedure Main();
var
customer: Tcustomers;
begin
customer := readCustomers('Read in customers');
end;
begin
Main();
end.
my code doesn't seem to work, ive tried implementing the code within the displayByDayPurchased procedure.
Well, in the code you've posted, your displayByDayPurchased doesn't actually implement anything which would result in matching records being displayed, all you have is an empty begin ... end block:
if (specific_day = customers.dayPurchased[specific_day])then
begin
end;
and a) that doesn't correctly specify the match condition anyway and b) it will not compile because a Tcustomers array does not have a dayPurchased field ( a Tcustomer record does, but not the array).
Your code depends of quite a lot of things whose definitions you have not provide (ReadString, ReadInteger, ReadIntegerRange, etc) so it is difficult to give you a tested solution.
However an implementation of your displayByDayPurchased should probably look something like this:
procedure displayByDayPurchased(customers:TCustomers);
var
specific_day:integer;
i : integer;
ADay : Tday;
begin
specific_day := ReadInteger('Enter day to see items purchased');
ADay := Tday(specific_day); // converts integer to TDay value
for i := Low(customers) to High(Customers) do begin
if customers[i].dayPurchased = ADay then begin
writenln(customers[i].itemPurchased);
end;
end;
end;
I assume your Tcustomer record actually includes the customer's name, andx your code needs modifying to handle that.
Btw, your function readDay(prompt:string): Tday is wrong; Because of your definition of Tday, the allowed values in readDay should be 0 and 1, because the lowest value of the Tday enumeration actually corresponds to zero, not 1.
Also, you did not say which Pascal compiler you are using, but most modern versions allow a call like Tday(integerValue) to convert an integer to an instance value of the enumeration.

Call by result Example

Is there any proper example for explaining call-by-result ? (not pseudocode)
I have learned that ALGOL 68, Ada could use this way,
but I cannot find any clear example of Call-by-Result.
I just made by myself.
pseudocode
begin
integer n;
procedure p(k: integer);
begin
n := n+1;
k := k+4;
print(n);
end;
n := 0;
p(n);
print(n);
end;
Implement using Ada Language
call.adb
with Gnat.Io; use Gnat.Io;
procedure call is
x : Integer;
Procedure NonSense (A: in out integer) is
begin
x := x + 1;
A := A + 4;
Put(x);
end NonSense;
begin
x := 0;
NonSense (x);
Put(" ");
Put(x);
New_Line;
end call;
Since Ada uses call-by-result way, result should be 1 4. (It could be checked by entering this code into online-Ada-compiler "http://www.tutorialspoint.com/compile_ada_online.php")
And, other result applied different passing parameter types should be...
call by value: 1 1
call by reference: 5 5
(compare > call by value-result: 1 4)

How to take a value from row 1, row 2, row 3 in column 3 using DBGrid?

In Delphi, I have a component:
dbgridsimpipa: Tdbgrid,
DSpipa: Tdatasource,
Qpipa: Tquery,
btnViewdata: tbutton
btnRecNumber: tbutton.
I search the table with this code:
procedure TfrmMain.btnViewDataClick(Sender: TObject);
var
st,st4,y0,y1,y2: string;
begin
st:='5';
grid2.Enabled := True;
st5 := 'SELECT tblRekapROT.CY2IDR, tblRekapROT.Produk, tblRekapROT.Total, tblRekapROT.ARRWL, tblRekapROT.ARBYL, tblRekapROT.Status '+
'FROM tblRekapROT '+
'WHERE ((tblRekapROT.Status Like ''%'+st+'%''))';
Qpipa.Close;
Qpipa.SQL.Clear;
Qpipa.SQL.Add(st5);
Qpipa.Open;
label22.Caption:= dbgridsimpipa.Fields[0].AsString;
end;
Then I count the records in DBGrid with :
procedure TfrmMain.btnRecNumberClick(Sender: TObject);
var
vCount: Integer;
vBookmark: TBookmark;
begin
vCount := 0;
with DBGridSimPipa.DataSource.DataSet do
begin
vBookmark := GetBookmark;
First; // move to the first record
while not EoF do
begin
vCount := vCount + 1;
Next; // move to next record
end;
GotoBookmark(vBookmark);
FreeBookmark(vBookmark);
end;
ShowMessage('We found ' + IntToStr(vCount) + ' records');
end;
How do I get the value of rows in DBGrid in one column? Maybe like this:
//label1.Caption:= dbgridsimpipa.DataSource.DataSet.First //record vtotal row 1
//label2.Caption:= dbgridsimpipa.DataSource.DataSet.second //record vtotal row 2
//label3.Caption:= dbgridsimpipa.DataSource.DataSet.third //record vtotal row 3
This a screenshot of my program.
First of all welcome to StackOverflow.
You have some misunderstanding about how dataset works. First of all if you want to know how many rows you found you can just call the RecordCound property:
procedure TfrmMain.btnRecNumberClick(Sender: TObject);
begin
ShowMessage('We found ' + IntToStr(DBGridSimPipa.DataSource.DataSet.RecordCount) + ' records');
end;
If you want to Access the first row in a Dataset you can call First but there are no Second or Third property so there you'll have to ude the property RecordNo
DataSet.First is the same as Dataset.RecordNo := 0
The second row you get by calling Dataset.RecordNo := 1 etc.
THen you wanted to put then content of a row into a label. There are no build in way of doing that, so you'll have to write you own function to to this.
function DataRowToString(const aDataset: TDataSet; aRecNo: Integer): String;
var
i: Integer;
begin
aDataset.RecNo := aRecNo;
Result := '';
for i := 0 to aDataset.FieldCount - 1 do
Result := Result + aDataset.Fields[i].AsString + ' ';
Result := Trim(Result);
end;
Finally we just need to call the newly created function:
label1.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 0); //record vtotal row 1
label2.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 1); //record vtotal row 2
label3.Caption:= DataRowToString(dbgridsimpipa.DataSource.DataSet, 2);//record vtotal row 3