| View previous topic :: View next topic |
| Author |
Message |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Mon Jan 07, 2002 9:11 pm Post subject: Comparing Text Files, Line-by-Line |
|
|
What's the best way to approach this?
I am using an NT utility with one of my VDS 3.51 projects, which outputs a tab-delimited text file, with one record per line.
My VDS project is running this utility every fifteen minutes. Each start of the cycle, it copies the current output file from the project folder to an archive folder, then runs the utility. I then compare the two files to see if there are any differences and act accordingly.
This is working fine, but I would like to refine it further...
I would like to compare the current file with the archive file, line-by-line and remove any duplicated entries in the current file.
Help?  |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Mon Jan 07, 2002 10:18 pm Post subject: |
|
|
I don't know if this will help, but if you can use a hidden
SORTED list (LIST CREATE, 1, SORTED), it will remove
duplicate entries. This won't help much if you're comparing
two files though.
Otherwise you'll have to loop thru the archive list, and check
the current list for each item. Something like this maybe:
| Code: |
LIST CREATE, 1
LIST CREATE, 2
LIST LOADFILE, 1, archive.txt
LIST LOADFILE, 2, current.txt
%x = 0
REPEAT
%y = 0
REPEAT
if @equal(@item(1, %x), @item(2, %y))
LIST DELETE, 2
end
%y = @succ(%y)
UNTIL @equal(%y, @count(2))
%x = @succ(%x)
UNTIL @equal(%x, @count(1))
LIST SAVEFILE, 2, current.txt
|
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Mon Jan 07, 2002 10:58 pm Post subject: |
|
|
Mac
Thanks for your answer...looks like "just the job"!
Will try it out and get back to you.
Thanks again
Dave
| Mac wrote: |
I don't know if this will help, but if you can use a hidden
SORTED list (LIST CREATE, 1, SORTED), it will remove
duplicate entries. This won't help much if you're comparing
two files though.
Otherwise you'll have to loop thru the archive list, and check
the current list for each item. Something like this maybe:
| Code: |
LIST CREATE, 1
LIST CREATE, 2
LIST LOADFILE, 1, archive.txt
LIST LOADFILE, 2, current.txt
%x = 0
REPEAT
%y = 0
REPEAT
if @equal(@item(1, %x), @item(2, %y))
LIST DELETE, 2
end
%y = @succ(%y)
UNTIL @equal(%y, @count(2))
%x = @succ(%x)
UNTIL @equal(%x, @count(1))
LIST SAVEFILE, 2, current.txt
|
Cheers, Mac
|
|
|
| Back to top |
|
 |
Garrett Moderator Team
Joined: 04 Oct 2001 Posts: 2149 Location: A House
|
Posted: Tue Jan 08, 2002 6:29 am Post subject: |
|
|
Hey Dave99, if you get something working on that, would you be willing to share that? I'm in serious need of a file compare routine also. Just haven't had the time to experiment with such myself yet. _________________ 'What you do not want done to yourself, do not do to others.' - Confucius (550 b.c. to 479 b.c.) |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Tue Jan 08, 2002 10:34 pm Post subject: |
|
|
Mac
Whereas my tests on dummy files work successfully, the output from the utility I mentioned fail...
The reason may lie in the length of the text from each line/record in the output, which is over 300 characters. I have read in the help, that the length is only limited by memory?
I guess the answer would be to limit the length of each line read. I am only interested in the date and time of the entry, so I could possibly use the @LEN() function, coupled with @SUBSTR().
Hope I've got this right?
Thanks for all your help!
Dave
| dave99 wrote: |
Mac
Thanks for your answer...looks like "just the job"!
Will try it out and get back to you.
Thanks again
Dave
| Mac wrote: |
I don't know if this will help, but if you can use a hidden
SORTED list (LIST CREATE, 1, SORTED), it will remove
duplicate entries. This won't help much if you're comparing
two files though.
Otherwise you'll have to loop thru the archive list, and check
the current list for each item. Something like this maybe:
| Code: |
LIST CREATE, 1
LIST CREATE, 2
LIST LOADFILE, 1, archive.txt
LIST LOADFILE, 2, current.txt
%x = 0
REPEAT
%y = 0
REPEAT
if @equal(@item(1, %x), @item(2, %y))
LIST DELETE, 2
end
%y = @succ(%y)
UNTIL @equal(%y, @count(2))
%x = @succ(%x)
UNTIL @equal(%x, @count(1))
LIST SAVEFILE, 2, current.txt
|
Cheers, Mac
|
|
|
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Tue Jan 08, 2002 10:38 pm Post subject: |
|
|
Garrett
Sure...no problem.
I'm not in the same league as some of you guys, but I have used VDS in some unusual projects!
Best
Dave
| Garrett wrote: |
Hey Dave99, if you get something working on that, would you be willing to share that? I'm in serious need of a file compare routine also. Just haven't had the time to experiment with such myself yet.
|
|
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Tue Jan 08, 2002 10:43 pm Post subject: |
|
|
Hey Dave,
In earlier versions of VDS, there was a 255 char limit on
string lists, but this shouldn't be a problem with VDS3
or later.
Did you try the test utility with the regular files you'll be
using? If it works with them, there may be an error
elsewhere in your main program.
Cheers, Mac _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Tue Jan 08, 2002 10:52 pm Post subject: |
|
|
Mac
Yes...I did try the test utility with the regular files and put an INFO display in between the IF clause to see what the output was. I got no displays, indicating there was no match, even when I tried with two identical text files!
Confused?
Dave
| Mac wrote: |
Hey Dave,
In earlier versions of VDS, there was a 255 char limit on
string lists, but this shouldn't be a problem with VDS3
or later.
Did you try the test utility with the regular files you'll be
using? If it works with them, there may be an error
elsewhere in your main program.
Cheers, Mac
|
|
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Tue Jan 08, 2002 11:25 pm Post subject: |
|
|
Dave, try this one on your regular files and let me know
what happens. Remember that any different dates, times,
etc. won't show up as the same entries. If this is a factor
you'll have to compare substrings.
Also, how large are the files you're comparing?
__________________________________________________________________________________________________________________________
| Code: |
rem -- VDS3 and VDS4 compatible --
rem -- Simple file compare program --
OPTION SCALE, 96
OPTION DECIMALSEP, "."
TITLE By Mac
DIALOG CREATE,Test prog,-1,0,300,220
DIALOG ADD,BUTTON,LoadFile1,5,5,60,20
DIALOG ADD,BUTTON,LoadFile2,5,70,60,20
DIALOG ADD,BUTTON,Compare,5,135,60,20
DIALOG ADD,LIST,L1,30,5,290,165
rem -- Put horizontal scroll on list --
%z = @sendmsg(@winexists(~L1),$0194,2000,0)
DIALOG ADD,STATUS,Stat
DIALOG SHOW
LIST CREATE, 1
LIST CREATE, 2
:EVLOOP
DIALOG SET, Stat, "Ready..."
WAIT EVENT
goto @event()
:LoadFile1BUTTON
%%f1 = @filedlg()
if %%f1
LIST LOADFILE, 1, %%f1
end
goto EVLOOP
:LoadFile2BUTTON
%%f2 = @filedlg()
if %%f2
LIST LOADFILE, 2, %%f2
end
goto EVLOOP
:CompareBUTTON
LIST CLEAR, L1
DIALOG SET, Stat, "Comparing files..."
%x = 0
REPEAT
if @item(1,%x)
%y = 0
REPEAT
if @equal(@item(1, %x), @item(2, %y))
LIST ADD, L1, [File 1 line @succ(%x) File 2 line @succ(%y)] @item(1, %x)
end
%y = @succ(%y)
UNTIL @equal(%y, @count(2))@greater(%y, @count(2))
end
%x = @succ(%x)
UNTIL @equal(%x, @count(1))@greater(%x, @count(1))
if @greater(1, @count(L1))
LIST ADD, L1, "No match found..."
end
goto EVLOOP
:CLOSE
EXIT
|
[EDIT]
Added code to eliminate checking blank lines.
Incremented x and y to reflect true line count.
Added @greater() to UNTIL loops to prevent errors. _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361

Last edited by Mac on Tue Jan 22, 2002 7:03 pm; edited 6 times in total |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Tue Jan 08, 2002 11:50 pm Post subject: |
|
|
Mac
Thanks for your efforts...will try your suggestions tomorrow (it's late here) and post back.
Best
Dave |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Jan 09, 2002 12:03 am Post subject: |
|
|
Dave, I modified the code to eliminate blank lines and
added line numbers to list L1.
Garrett, it's just a simple test program, but maybe
you can use it as a start for a file compare program.
Cheers, Mac
[EDIT]
Incremented x and y to reflect true line count in List L1. _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Wed Jan 09, 2002 9:39 pm Post subject: |
|
|
Mac
The problem seems to lie with the line:
if @equal(@item(1, %x), @item(2, %y))
If I change it to:
if @equal(@Substr(@item(1, %x),1,20), @Substr(@item(2, %y),1,20))
it then evaluates correctly and everything works!
So, is it the @Equal function which is having trouble with long text strings?
Anyway, this seems to work for me, so thanks again for all your help and encouragement!
Best
Dave |
|
| Back to top |
|
 |
Mac Professional Member

Joined: 08 Jul 2000 Posts: 1585 Location: Oklahoma USA
|
Posted: Wed Jan 09, 2002 11:06 pm Post subject: |
|
|
Hey Dave,
"@equal()" doesn't have a problem with long text strings that I'm
aware of, but it's possible...
Would it be asking too much for you to visually compare a couple
of the strings from the two files? You can add them both to a list,
notepad, wordpad or whatever (one right above the other) and
check each char to make sure there are no differences. Even a
@cr() or a space at the end will make a difference.
If they appear to be the same, post them and I'll try them
on my system in case "@equal" is acting different on NT.
If @equal() has a problem, we really need to know about it.
Thanks, Mac  _________________ VDSug.dll does file IO, check/disable menu items,
non-VDS dlls, draw functions and more...
Free download (30k dll size) at:
http://www.vdsworld.com/download.php?id=361
 |
|
| Back to top |
|
 |
moke Contributor

Joined: 02 Jan 2002 Posts: 162
|
Posted: Thu Jan 10, 2002 4:25 am Post subject: |
|
|
Dave,
I don't know if this helps you or not but how about using @match(list,%%string). @match will return true as long as the string exists within some part of a line item of your list. Plus you don't need to fiddle with substrings or worry about the length of your string.
For example:
| Code: |
list create,1
list add,1, 01/01/02,data1,data2,data3
list add,1, 01/02/02,data1,data2,data3
list add,1, 01/03/03,data1,data2,data3
list seek,1,0
if @match(1,01/02/02)
info found match
end
|
moke |
|
| Back to top |
|
 |
dave99 Newbie
Joined: 15 Jun 2001 Posts: 24 Location: Torquay, Devon, United Kingdom
|
Posted: Mon Jan 21, 2002 11:42 pm Post subject: |
|
|
Mac
Apologies for not getting back sooner...
It appears there is no issue with @Equal() function and long strings (perhaps a low memory issue with my laptop and the number of applications open in the office).
What I am experiencing with the code you kindly posted is when two files are identical. The second list containing the previous log results naturally becomes empty, but the %y parameter continues to count up and does not stop. I have modified the code in an example app (using If @Zero(@Count()) functions as follows:
title Compare Files
DIALOG CREATE,Compare Files,-1,0,389,210
DIALOG ADD,LIST,LIST1,4,12,,,CLICK
DIALOG ADD,LIST,LIST2,4,190
DIALOG ADD,STATUS,STATUS
DIALOG SHOW
list loadfile,list1,lockout2.log
list loadfile,list2,lockout.log
:evloop
wait event
goto @event()
:LIST1CLICK
%x = 0
REPEAT
%y = 0
REPEAT
If @Equal(@item(List1, %x), @item(List2, %y),EXACT)
LIST DELETE, List2
end
If @Zero(@Count(List2))
Goto EvLoop
End
%y = @succ(%y)
UNTIL @equal(%y, @count(List2))
%x = @succ(%x)
UNTIL @equal(%x, @count(List1))
goto evloop
:CLOSE
exit
Do you anticipate any problems with this?
Thanks again
Dave
| Mac wrote: |
Hey Dave,
"@equal()" doesn't have a problem with long text strings that I'm
aware of, but it's possible...
Would it be asking too much for you to visually compare a couple
of the strings from the two files? You can add them both to a list,
notepad, wordpad or whatever (one right above the other) and
check each char to make sure there are no differences. Even a
@cr() or a space at the end will make a difference.
If they appear to be the same, post them and I'll try them
on my system in case "@equal" is acting different on NT.
If @equal() has a problem, we really need to know about it.
Thanks, Mac
|
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You can attach files in this forum You can download files in this forum
|
|