SRL Archive - SRL Archives

function InChatMulti(Text: TStringArray): Boolean;


BobboHobbo

2008-07-15 09:31:56

Same as InChat just with a TStringArray.

Use: InChatMulti(['hello','hi','yo','sup','hey'])

function InChatMulti(Text: TStringArray): Boolean;
var
s: string;
i, n: Integer;
begin
if (not (GetLastChatText(s))) then
LastChatter(s);
n := Length(Text) - 1;
for i := 0 to n do
if (Pos(Text[i], s) <> 0) then
begin
Result := True;
Exit;
end;
end;

Daniel

2008-07-15 21:53:36

Here's one i made a while ago ;)
function InChatMulti(Text: TStringArray): Boolean;
var
i: Integer;
begin
Result:=True;
for i:=0 to High(Text) do
if(InChat(Text[i])) then
Exit;
Result:=False;
end.

BobboHobbo

2008-07-15 22:10:56

n00b dont be a smart one :(

EDIT** its the same n00b you just have Inchat again n00b :P

Daniel

2008-07-15 22:15:52

From: BobboHobbo
n00b dont be a smart one :(

EDIT** its the same n00b you just have Inchat again n00b :P

Huh? If there is already an InChat function in SRL, then why call it again? It can be made much more shorter ;)

ZephyrsFury

2008-07-15 22:20:56

[QUOTE=Dan's The Man;433246]Huh? If there is already an InChat function in SRL, then why call it again? It can be made much more shorter ;)[/QUOTE]

You have to get the text again for each string in the array. This is ok for small arrays but for autoresponders where you have something like 20 phrases it will lag quite a bit.

Nava2

2008-08-20 08:52:14

function InChatArray(Text: TStringArray): Boolean;

var
i, H: integer;
s: string;

begin
H := High(Text);
for i := 0 to H do
begin
if (not (GetLastChatText(s))) then LastChatter(s);
Result := (Pos(Lowercase(Text[i]), (Lowercase(s)) > 0);
if Result then Break;
end;
end;

Here's mine. A little cleaner, faster, and uses Lowercase to check case errors. :)

P.S:
[QUOTE=Dan's The Man;433231]Here's one i made a while ago ;)
function InChatMulti(Text: TStringArray): Boolean;
var
i: Integer;
begin
Result:=True;
for i:=0 to High(Text) do
if(InChat(Text[i])) then
Exit;
Result:=False;
end.


You know that scar automatically sets all boolean variables to false right? So having result := false at the end is really useless.

Daniel.Blaze

2008-08-20 09:20:03

He set result := true at the top :duh:

[php]
function InChatArray(Text: TStringArray): Boolean;

var
i, H: integer;
s: string;

begin
H := High(Text);
if (not (GetLastChatText(s))) then LastChatter(s);
for i := 0 to H do
begin
Result := (Pos(Lowercase(Text), (Lowercase(s)) > 0);
if Result then Break;
end;
end;
[/php]

That's better.

Nava2

2008-08-20 09:50:31

From: Daniel.Blaze
He set result := true at the top :duh:

[php]
function InChatArray(Text: TStringArray): Boolean;

var
i, H: integer;
s: string;

begin
H := High(Text);
if (not (GetLastChatText(s))) then LastChatter(s);
for i := 0 to H do
begin
Result := (Pos(Lowercase(Text), (Lowercase(s)) > 0);
if Result then Break;
end;
end;
[/php]

That's better.


Thanks, that speeds it up! :D

EvilChicken!

2008-08-20 09:58:56

Ok, since this has turned into some sort of competition, allow me to join. And win :P

function InChatArray(Text: array of string): Boolean;
var
I, H: integer;
S: string;

begin
Result := False;
H := High(Text);
if not GetLastChatText(S) then LastChatter(s);
for I := 0 to H do
if Pos(Lowercase(Text[I]), Lowercase(S)) > 0 then
begin
Result := True;
Exit;
end;
end;

My function may be fractions of a millisecond faster than some of yours, but..
Fractions are fractions.

Three things: TStringArray -> array of string: TStringArray is defined in WizzyPlugin, while an array of string is defined in SCAR, your interpreter, as an array of char. You may (or may not) save upto 10 MS (:eek:) by removing the need to take the trip to WizzyPlugin for a check if you already have something you can use instead.

Furthermore, I believe that defining the result in a loop should slow down a procedure/function. (Daniel.Blaze) I'm not sure, but that is only what common logic tells me. And, the code stayed the sme length because I removed a useless begin/end, and finally, I replaced the "Break" with "Exit", since there is no difference.

Nava2

2008-08-20 10:45:17

From: EvilChicken!
Ok, since this has turned into some sort of competition, allow me to join. And win :P

function InChatArray(Text: array of string): Boolean;
var
I, H: integer;
S: string;

begin
Result := False;
H := High(Text);
if not GetLastChatText(S) then LastChatter(s);
for I := 0 to H do
if Pos(Lowercase(Text[I]), Lowercase(S)) > 0 then
begin
Result := True;
Exit;
end;
end;

My function may be fractions of a millisecond faster than some of yours, but..
Fractions are fractions.

Three things: TStringArray -> array of string: TStringArray is defined in WizzyPlugin, while an array of string is defined in SCAR, your interpreter, as an array of char. You may (or may not) save upto 10 MS (:eek:) by removing the need to take the trip to WizzyPlugin for a check if you already have something you can use instead.

Furthermore, I believe that defining the result in a loop should slow down a procedure/function. (Daniel.Blaze) I'm not sure, but that is only what common logic tells me. And, the code stayed the sme length because I removed a useless begin/end, and finally, I replaced the "Break" with "Exit", since there is no difference.


Bugger, all you did was change two things :(. HA BUT I WILL WIN! FOUND SOMETHING WOOT.

{*******************************************************************************
function InChatArr(Text: Array of String) : Boolean;
By: WT-Fakawi & Nava2
Description: Grabs Last Chat Line and performs checks on occurence of text
*******************************************************************************}

function InChatArr(Text: Array of String): Boolean;

var
I, H : integer;
s : string;

begin
H := High(Text);
if (not (GetLastChatText(s))) then LastChatter(s);
for I := 0 to H do
begin
if (Pos(Lowercase(Text[I]), (Lowercase(s)) > 0) then
begin
Result := true;
Exit;
end;
end;
end;

Muhahah mine is faster, you don't have to define Result as false. SCAR automatically says its false until its defined as true.

EvilChicken!

2008-08-20 10:53:24

Now you're just being stubborn or whatever =.=`

The result always needs to be defined.
Almighty Markus (ph3ar h1m) showed me a couple SCAR bugs where the results in procedures and functions weren't nil'ed or reset. If you look at SRL functions, most of them already have "Result := False/0/whatever" at the very start.

And, you should try to avoid unneeded begins&ends. :)

Nava2

2008-08-20 11:01:38

From: EvilChicken!
Now you're just being stubborn or whatever =.=`

The result always needs to be defined.
Almighty Markus (ph3ar h1m) showed me a couple SCAR bugs where the results in procedures and functions weren't nil'ed or reset. If you look at SRL functions, most of them already have "Result := False/0/whatever" at the very start.

And, you should try to avoid unneeded begins&ends. :)


Eek, when I changed how the result was defined I forgot to remove the begin and end :(

Edit:

Run this:
program new;
var
boo : boolean;
i : integer;

begin
for i := 0 to 10 do
Writeln(booltostr(boo));
end.

This is my result:
False
False
False
False
False
False
False
False
False
False
False
Successfully executed

Wizzup?

2008-08-20 11:04:19

From: Nava2
Muhahah mine is faster, you don't have to define Result as false. SCAR automatically says its false until its defined as true.


Yours seriously can't be faster, at least your first version… If you call
If Not GetLastChatText(s) Then lastChatter(s)
everytime in a loop, it will tremendously slower. So calling it once before you enter the loop is a lot smarter…

Nava2

2008-08-20 11:06:50

From: Wizzup?
Yours seriously can't be faster, at least your first version… If you call
If Not GetLastChatText(s) Then lastChatter(s)
everytime in a loop, it will tremendously slower. So calling it once before you enter the loop is a lot smarter…


We fixed that. But, I figured mine might be like a fraction of a second faster because mine doesn't declare result := false at the beginning.

Btw, the most recent one is the most recent post ;)

Naum

2008-08-20 12:06:08

Why not use a flaming timemark to see, If you really are so passionate about this. God!

OnTopic : Couldn't you use an or statement work the same way, sure it will be a little slower but it will do the same thing?

Nava2

2008-08-20 15:01:16

From: NaumanAkhlaQ
Why not use a flaming timemark to see, If you really are so passionate about this. God!

OnTopic : Couldn't you use an or statement work the same way, sure it will be a little slower but it will do the same thing?


Yes, but it could be a lot of them, as well, the repeating of InChat will be slow.