Solved

Comparing elements in a VarArray

Posted on 2000-05-04
11
727 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now