Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 739
  • Last Modified:

Comparing elements in a VarArray

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
Stuart_Johnson
Asked:
Stuart_Johnson
1 Solution
 
shenqwCommented:
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
 
LischkeCommented:
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
 
Stuart_JohnsonAuthor Commented:
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
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
DragonSlayerCommented:
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
 
LischkeCommented:
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
 
Stuart_JohnsonAuthor Commented:
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
 
LischkeCommented:
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
 
Stuart_JohnsonAuthor Commented:
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
 
LischkeCommented:
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
 
Stuart_JohnsonAuthor Commented:
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
 
LischkeCommented:
You are welcome, Stu. I'm pleased that it is now even better working than you expected before :-)

Ciao, Mike
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Tackle projects and never again get stuck behind a technical roadblock.
Join Now