Solved

Comparing elements in a VarArray

Posted on 2000-05-04
11
726 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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

757 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

21 Experts available now in Live!

Get 1:1 Help Now