SRL Archive - SRL Suggestions

InVarArray(Arr: TVariantArray; Value: Integer);


Coh3n

2009-12-12 19:52:21

InVarArray(Arr: TVariantArray; Value: Integer);


Just a simple function I needed when writing a progress report. Can be useful for many things.


{*******************************************************************************
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
By: Coh3n
Description: Returns true if 'Value' is found in a Variant array.
*******************************************************************************}
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
try
Result := Arr[i] = Value;
finally
if Result then
Exit;
end;
end;
Thanks to Icefire for the idea, and Nadeem for writing the following function.

{*******************************************************************************
function InVarArrayEx(var Index: Integer; Arr: TVariantArray; Value: Variant): Boolean;
By: Nadeem
Description: Returns true if 'Value' is found in a Variant array, and returns the index ('Index') of the
value.
*******************************************************************************}
function InVarArrayEx(var Index: Integer; Arr: TVariantArray; Value: Variant): Boolean;
var
h: Integer;
begin
h := high(arr);
for Index := 0 to h do
try
Result := Arr[Index] = Value;
finally
if Result then
Exit;
end;
end;

Thanks to Sex for the Runtime Error fix.

I guess noidea beat me to it a while ago -> http://www.villavu.com/forum/showthread.php?t=49084&highlight=InArr

Sex

2009-12-12 19:57:37

You need
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
begin
try Result := Arr[i] = Value except end;
if Result then
Exit;
end;
end;
Because if you have the VariantArray containing different types of data then it will error.

Such as:
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
begin
Result := Arr[i] = Value;
if Result then
Exit;
end;
end;

var
TVA : TVariantArray;
begin
TVA := ['b', 2, true, 3];
Writeln(BoolToStr(InVarArray(TVA, 3)));
end.

Coh3n

2009-12-12 20:09:34

Thanks! :) Guess it never occurred to me that it may error since it's worked for me every time.

noidea

2009-12-12 20:10:28

http://www.villavu.com/forum/showthread.php?t=49084&highlight=InArr()

And you're going to need a try catch statement. What if someone tries comparing a string against an int?

Coh3n

2009-12-12 20:13:58

From: noidea
http://www.villavu.com/forum/showthread.php?t=49084&highlight=InArr()

And you're going to need a try catch statement. What if someone tries comparing a string against an int?

Ah, looks like you beat me to it. ;)

And yeah, I've never used try..except statements before.

Sir R. M8gic1an

2009-12-13 05:21:33

should be pluginized….

Get nava to re-write it for wizzypluggin.

:)

~RM

Naum

2009-12-13 05:27:14

However, the beauty of a try..except is where you can have a Finally, turning this:

{*******************************************************************************
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
By: Coh3n
Description: Returns true if 'Value' is found in a Variant array.
*******************************************************************************}
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
begin
try
Result := Arr[i] = Value;
except end;

if Result then
Exit;
end;
end;

Into:

{*******************************************************************************
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
By: Coh3n
Description: Returns true if 'Value' is found in a Variant array.
*******************************************************************************}
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
try
Result := Arr[i] = Value;
Finally
if Result then
Exit;
except end;
end;

nielsie95

2009-12-13 05:38:06

No need for Wizzyplugin:


function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
{$IFNDEF SCAR320_UP}
var
h, i: Integer;
{$ENDIF}
begin
{$IFDEF SCAR320_UP}
try
Result := Value in Arr;
except
Result := False;
end;
{$ELSE}
Result := False;
h := High(Arr);
for i := 0 to h do
try
if (Arr[i] = Value) then
begin
Result := True;
Break;
end;
except end;
{$ENDIF}
end;

EDIT: I guess it doesn't work exactly the same.. as it would return false in the following:


InVarArray([1, 2, 3, '4', 'abc', 5], 5)

Because it would throw an error with 'abc', this could be fixed by searching for '5', but nevertheless it's not exactly the same. I wanted to show it off anyway, because many of you probably didn't know about in.

Coh3n

2009-12-13 10:36:34

From: Naum
However, the beauty of a try..except is where you can have a Finally, turning this:

{*******************************************************************************
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
By: Coh3n
Description: Returns true if 'Value' is found in a Variant array.
*******************************************************************************}
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
begin
try
Result := Arr[i] = Value;
except end;

if Result then
Exit;
end;
end;

Into:

{*******************************************************************************
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
By: Coh3n
Description: Returns true if 'Value' is found in a Variant array.
*******************************************************************************}
function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
var
h, i: Integer;
begin
h := High(Arr);
for i := 0 to h do
try
Result := Arr[i] = Value;
Finally
if Result then
Exit;
except end;
end;

Well, I know basically nothing about try..except..finally statements, so I'm probably wrong, but wouldn't what you changed just do the same thing since if there's an exception, it ends anyway?

Sir R. M8gic1an

2009-12-13 10:40:08

you might as wel just leave the except end out since you have finally…

~RM

Coh3n

2009-12-13 10:44:02

From: Rasta Magician
you might as wel just leave the except end out since you have finally…

~RM

That was another thing I was kind of confued about because I was reading some pascal references on try statements, and they treat the try..except and try..finally as separate statements, not one.

Wanted

2009-12-13 11:43:10

Make InVarArrayEx too where it returns index number.

Nava2

2009-12-13 12:00:53

try..except..finally..end. Thats how it goes. Always put except first, but if you leave it out, it doesn't matter. As long as you have finally or except the statement will be fine.

try 
DoSomething;
except
finally
DosomethingElse;
end

Better:
try
DoSomething;
finally
DoSomethingElse;
end;

See what I'm sayin' bra?

Nadeem

2009-12-13 12:46:12

this is probably what i'd cough up for this:

function InArr(var index: Integer; arr: TVariantArray; val: Variant): Boolean;
var h: Integer;
begin
h := high(arr);
for index := 0 to h do
try
Result := arr[index] = val;
finally
if (Result) then Exit;
end;
end;

would return the index found at aswell…



~NS

Coh3n

2009-12-13 20:07:37

From: Nava2
See what I'm sayin' bra?

Lol yes, I do. :)

Nadeem & Icefire, I've edited the first post.

Gu3st...

2009-12-14 08:19:20

From: nielsie95
because many of you probably didn't know about in.
Wow that is so cool! Awesome :o

Also the function itself is quite useful, I say commit/wizzyplugin

Sex

2009-12-14 16:30:16

From: nielsie95
No need for Wizzyplugin:

Code:
[LEFT]function InVarArray(Arr: TVariantArray; Value: Variant): Boolean;
{$IFNDEF SCAR320_UP}
var
h, i: Integer;
{$ENDIF}
begin
{$IFDEF SCAR320_UP}
try
Result := Value in Arr;
except
Result := False;
end;
{$ELSE}
Result := False;
h := High(Arr);
for i := 0 to h do
try
if (Arr = Value) then
begin
Result := True;
Break;
end;
except end;
{$ENDIF}
end;
[/LEFT]


EDIT: I guess it doesn't work exactly the same.. as it would return false in the following:

Code:
[LEFT]InVarArray([1, 2, 3, '4', 'abc', 5], 5)
[/LEFT]


Because it would throw an error with 'abc', this could be fixed by searching for '5', but nevertheless it's not exactly the same. I wanted to show it off anyway, because many of you probably didn't know about in.

Damn, I thought in was in Delphi only.

Coh3n

2009-12-17 16:43:03

From: Zyt3x
Also the function itself is quite useful, I say commit/wizzyplugin

If it does get committed, I think the credit should be given to noidea considering he came up with it first. :p