Solved

Comparing elements in a VarArray

Posted on 2000-05-04
11
735 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
[X]
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
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Suggested Courses
Course of the Month9 days, 1 hour left to enroll

617 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