Comparison of two files

This is a discussion on Comparison of two files within the REXX forums in Programming Languages category; On Thu, 7 Aug 2008 21:54:44 UTC, Xizor <mn @ mn-ix.de> wrote: > Here's what I did: > > do while lines(dat1) <> 0 > origdat1 = lines(dat1) > origdat2 = lines(dat2) > if origdat1 == origdat2 then > write to file1 > else > write to file2 > end > > the write is call lineout of course. Well, I will throw some gasoline on the fire. This REXX program does not do exactly what you want but it may spark some thought. This will merge two files that have identical record layouts and are already sorted on the ...

Go Back   Application Development Forum > Programming Languages > REXX

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #11  
Old 08-08-2008, 11:10 PM
Gordon Snider
Guest
 
Default Re: Comparison of two files

On Thu, 7 Aug 2008 21:54:44 UTC, Xizor <mn@mn-ix.de> wrote:

> Here's what I did:
>
> do while lines(dat1) <> 0
> origdat1 = lines(dat1)
> origdat2 = lines(dat2)
> if origdat1 == origdat2 then
> write to file1
> else
> write to file2
> end
>
> the write is call lineout of course.


Well, I will throw some gasoline on the fire.
This REXX program does not do exactly what you want but it may spark
some thought.

This will merge two files that have identical record layouts and are
already
sorted on the same key fields. It is written in OS/2 Classic REXX. I
am not
familiar with Regina but I expect it will have similar features
available.
This will produce an output file containing all the records of the
input files.
This program has been only lightly tested.
All comments and criticism welcome.

WATCH OUT FOR LINE WRAPS.

--
Gordon Snider
Toronto, Canada


/* MERGE REXX CMD by Gord Snider v01.07 2007/07/04
*/
/* PURPOSE: To combine two preordered (sorted) ASCII text files with
identical record layouts and fixed length fields, into one ordered
file.
The input files must be ordered on the same key field(s).
The default merge is ascending keys, case sensitive which
may be changed per key field at the time of key specification.

SYNTAX: MERGE [/?]

Optional switch:
/? shows this help screen and exits.

The program will ask for:
1. the name of the merged output file.
2. the description of the key, (as described below).
3. the name of input file 1, which must be sorted on the key field(s);
4. the name of input file 2, which must be sorted on the key field(s).


Key Description
The key field(s) must be at the same location in both input files.
Enter the key fields thus, with commas between the entries, (and a
semicolon
between the key fields, if more than one field).
1. the first column of the key field,
2, the first column of the next field,
3. 'a' for an ascending key or 'd' for descending key,
4. 's' for case sensitive key or 'i' for case insensitive key.

The fully qualified name of the output file must NOT be the same as
the fully qualified name of either input file.

To have a properly merged output file you must merge on the same set
of
fields you sorted the inputs on.
*/

parse arg myargs '/'switches +0

opt. = 0 /* unset options will
be FALSE */
mod. = '' /* unset modifications
will be NULL */
do while pos( '/', switches) \= 0 /* each option must
have leading slash */
parse var switches '/'opt'/'switches +0 /* parse out the next
option/modification set */
parse upper var opt opt 2 mod /* split the option
from any mod */
opt.opt = 1 /* capture the option
value and set it to TRUE */
mod.opt = mod /* capture the
option's modification */
end

if opt.? then do /* Help screen */
do l = 1 to sourceline() while left( sourceline( l), 2) \= '*' ||
'/'
say sourceline( l)
if l // 22 = 0 then do
say ' Hit <Enter> to continue ...'
pull .
end
end l
exit
end /* if opt.? */

say 'What is to be the fully-qualified name of the merged output
file?'
parse pull file.0
if Stream( file.0, 'C', 'QUERY EXISTS') = '' then do
call Stream file.0, 'C', 'OPEN WRITE'
say Stream( file.0, 'C', 'QUERY EXISTS') result
end
else do
say 'File' file.0 'already exists.'
exit
end
say

say 'What is the key? col1,col2,a/d,i/s[;col1,col2,a/d,i/s...]'
parse upper pull keyarg 0 1 keyarg1 0
say

do i = 1 to 2
say 'What is the name of input file' i 'to merge?'
parse pull file.i
if Stream( file.i, 'C', 'OPEN READ') = "READY:" then
say Stream( file.i, 'C', 'QUERY EXISTS') result
else do
say 'Problem opening' file.i' RC =' result
exit
end
say
end i

/* This loop executes once per output record while both files have
records remaining , and a record will
be taken only from one input file, per loop. When all the records
have been used from one file then
this loop will complete and all the remaining records will be used
from the other file. */
/* Do the grossest comparison first, i.e. is there a record from
each file to be compared?
*/
do while Lines( file.1) & Lines( file.2) /* Do while
there are more input records ... */

/* At this point records must remain in BOTH input files.
'Pre-read' a record from each file. */
spot1 = Stream( file.1, 'C', 'SEEK +0') /* Save
location of current record. */
line1 = LineIn( file.1) /* Pre-read
record from file1 for testing key(s). */
call Stream file.1, 'C', 'SEEK ='spot1 /* Reset
read position to beginning of record. */

spot2 = Stream( file.2, 'C', 'SEEK +0') /* Save
location of current record. */
line2 = LineIn( file.2) /* Pre-read
record from file2 for testing key(s). */
call Stream file.2, 'C', 'SEEK ='spot2 /* Reset
read position to beginning of record. */

/* The keys must be compared to see which record to put in the
output first. */
/* Starting with the major key, check as many key fields as
necessary to make the decision. */
keyarg = keyarg1 /* Refresh
the keys for each comparison */
do while length( keyarg > 0)
parse var keyarg keycol1 ',' keycol2 ',' ad ',' si ';' keyarg

if si = 'I' then do /* Case
insensitive. */
parse upper var line1 =(keycol1) key1 =(keycol2) /* Get next
key field of record in file.1 */
parse upper var line2 =(keycol1) key2 =(keycol2) /* Get next
key field of record in file.2 */
end
else do /* Case
sensitive. */
parse var line1 =(keycol1) key1 =(keycol2)
parse var line2 =(keycol1) key2 =(keycol2)
end

/* Now check this key field to see if the tie is broken. */
if ad = 'D' then /* Keys
descending. */
if key1 > key2 then /* Compare
the keys. */
call LineOut file.0, LineIn( file.1) /* Feed the
output the next record from file 1 */
else if key1 < key2 then
call LineOut file.0, LineIn( file.2) /* Take
record with higher key. */
else if length( keyarg) > 0 then /* This key
has not broken the tie. */
iterate /* Check
the next key field. */
else call LineOut file.0, LineIn( file.1) /* It's a
tie! Arbitrary choice. */
else /* Keys
ascending. */
if key1 < key2 then /* Compare
the keys. */
call LineOut file.0, LineIn( file.1) /* Feed the
output the next record from file 1 */
else if key1 > key2 then
call LineOut file.0, LineIn( file.2) /* Take
record with lower key. */
else if length( keyarg) > 0 then /* This key
has not broken the tie. */
iterate /* Another
key field? */
else call LineOut file.0, LineIn( file.1) /* It's a
tie! Arbitrary choice. */

leave
end /* do while length( keyarg) > 0 */
end /* do while records */

do while Lines( file.1)
call LineOut file.0, LineIn( file.1) /* take the
remaining records from file.1. */
end

do while Lines( file.2)
call LineOut file.0, LineIn( file.2) /* take the
remaining records from file.2. */
end

call Stream file.0, 'C', 'CLOSE'
call Stream file.1, 'C', 'CLOSE'
call Stream file.2, 'C', 'CLOSE'

exit 0
Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:46 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.