Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Comparing elements in a VarArray

Posted on 2000-05-04
11
731 Views
Last Modified: 2012-06-27
Hi.

I do this:

var
   vOld, vNew: Variant;

begin
   vOld := VarArrayCreate([1, sOrg.Size], varVariant);
   vNew := VarArrayCreate([1, sNew.Size], varVariant);

{load in data - which, incidently, is two files - one in each array}

I then want to compare each element to check for indifferences.  If there ANYWAY I can do this?  I have tried using

if vNew[iPos] <> vOld[iPos] then;

and although it compiles, I get a runtime error saying "invalid variant operation".

Can anyone help please?


Stu
0
Comment
Question by:Stuart_Johnson
11 Comments
 
LVL 3

Expert Comment

by:shenqw
ID: 2779727
Maybe your variant is empty. so you can try this:(Maybe you change the code ,if you want varEmpty=varNull and so on)

 function CompareVariant(AVariant,BVariant:Variant):boolean;
 begin
   Result:=False;
   if (VarIsEmpty(AVariant)) and (VarIsEmpty(BVariant)) then
     Result:=True
   else if (not VarIsEmpty(AVariant)) and (not VarIsEmpty(BVariant)) then
     Result:=(AVariant=BVariant);
 end;
0
 
LVL 10

Accepted Solution

by:
Lischke earned 50 total points
ID: 2780579
Stu,

I wonder that you can compile this at all. What you are trying to do is: "I have something here and I have something there. I don't know what it is but please tell me if the unknown is equal" :-) This cannot work this way, obviously. I think you have to compare each possible variant type separately.

For instance:

  for each value in the variant arrays do:
  case VarType(Value) and varTypeMask of
    varByte:...
    varSmallInt:...
    varInteger:...
    varDate:...
    varSingle:...
    varDouble:...
    varBoolean:...
    varString:...
  else
    ShowError('Unable to compare');
  end;

You have to make sure you convert compareable values into a common datatype (e.g. by assigning it to a local variable). For this you need to implemented a kind of matrix which knows how to compare varByte with varSmallInt, varSingle with varDouble (these are the easy cases) up to varString with varBoolean etc. I think you got the idea.

Ciao, Mike
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 2785617
shenqw: The array is definately not empty as I can write the data to a file, and the file is exactly the same size as the file I read the data from.

Mike: I would love to type cast it as a Char, but there is no VarChar :(  I am reading raw data from a file, and I need to compare that to an older file.  The only reason I did it like this was because it was quick.  If you can suggest another way....


Stu.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 14

Expert Comment

by:DragonSlayer
ID: 2786039
how about this:

function CompareVariant(AVariant,BVariant:Variant): boolean;
 begin
   Result:=False;
   if (VarIsEmpty(AVariant)) and (VarIsEmpty(BVariant)) then
     Result:=True
   else if (not VarIsEmpty(AVariant)) and (not VarIsEmpty(BVariant)) then
     Result:=(AVariant.AsString =BVariant.AsString);
 end;


0
 
LVL 10

Expert Comment

by:Lischke
ID: 2786174
Stu, let me clarify one thing. You want to compare data which is stored in some files, but it is some variant data, correct? Could it perhaps be that the data as such is in no way variant related but just some binary stuff? If yes this would ease the comparison a lot. Can you give as a short summary how the data is stored in the file?

Ciao, Mike
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 2786751
Hi Mike,

The data is binary as you guessed.  And the sizes of the files can range from a few bytes to a few megabytes (less than 20).

Is there anything else you need to know?  If so, please ask.

Thank for the help,

Stu.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2787534
Aha! Why do you not just compare binary streams? It would be so easy like:

1) open two streams with data to compare
2) load two buffers of same size, one from first stream, one from the second
3) do a CompareMem (SysUtils.pas) to compare the buffers, stop loop if they differ
4) loop to 2 until one or both streams are exhausted

This is so easy. Of course you can seek to specific start points in 1) and limit to certain sizes in 4).

What do you think about this approach?

Ciao, Mike
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 2788104
Sounds pretty damn fine to me, Mike!

I'll give it a shot and see what I can come up with.  The VarArrays where attractive because I could fill them so quickly and limit disk/network activity, but I guess I can make a relatively large buffer, and that will work just as well.

Give me a bit and I'll get back to you.

Thanks a million!

Stu.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2788130
I haven't mentioned the best fact about the binary approach yet: It's the fastest of all possiblities. Btw: The buffer must not be very large. 64K should be very fine. To make it even better you can use memory mapped files (CreateFileMapping), but this a different story.

Ciao, Mike
0
 
LVL 6

Author Comment

by:Stuart_Johnson
ID: 2837356
Hi Mike,

Sorry I took so long to get back to you on this - I thought I had already graded it :)

Everything is working just fine now!  And the compare is super fast (which is brilliant!).

Thanks a lot,

Stu.
0
 
LVL 10

Expert Comment

by:Lischke
ID: 2837404
You are welcome, Stu. I'm pleased that it is now even better working than you expected before :-)

Ciao, Mike
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

861 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question