I have a 4000 fixed length file in whcih the fields are “|” delimited .I need to COMPRESS the fields ans make it variable length “|” delimited.Currently i am not able to use EXAMINE and COMPRESS for the same as the maximum length allowed in this version for an elementary variable in 253 . Alpha-253 and EXAMINE and COMPRESS does not work on group vairable .
How about:
DEFINE DATA LOCAL
1 #RECORD (A4000)
1 #A (A/2000) DYNAMIC
1 #B (A) DYNAMIC
END-DEFINE
*
READ WORK FILE 1 #RECORD
SEPARATE #RECORD INTO #A(*) WITH DELIMITERS '|'
COMPRESS #A(*) INTO #B WITH DELIMITERS '|'
WRITE WORK FILE 2 VARIABLE #B
END-READ
END
Natural 3.1! Yikes!! Jerome seems to have missed that detail, because DYNAMIC variables weren’t available until Natural 4.
Here’s a solution that uses a vector, but be warned that it will burn a bit of CPU. I used Natural 6.3, so you’ll have to verify its results in your own environment - I offer neither guarantees nor warranties.
DEFINE DATA LOCAL
1 #MAX (I4) CONST <50> /* record length
1 #R (A1/#MAX)
1 REDEFINE #R /* debug
2 #REC (A50) /* debug
1 #T (A1/#MAX)
1 #I (I4)
1 #J (I4)
1 #C (I4)
END-DEFINE
DEFINE WORK FILE 1 'c:\ProgramData\Software AG\Natural\Natapps\Fuser\'
- 'Open-Src\Res\COMPREC.TXT'
READ WORK 1 #R (*)
WRITE 'Before:' #REC /* debug
/*
FOR #I = 1 #MAX /* right-to-left
ASSIGN #J = #MAX - #I + 1
ASSIGN #T (#J) = #R (#I)
END-FOR
/*
ASSIGN #I = 1
ASSIGN #J = #MAX
REPEAT /* compress
IF #T (#I) = '|'
AND #T (#I + 1) = ' '
THEN
ASSIGN #T (#I + 1 : #MAX - 1) = #T (#I + 2 : #MAX)
RESET #T (#MAX)
ELSE
ADD 1 TO #I
END-IF
IF #I >= #MAX
OR #I >= #J
THEN
ESCAPE BOTTOM
END-IF
END-REPEAT
/*
FOR #C = #MAX 1 -1 /* record length
IF #T (#C) <> ' '
THEN
ESCAPE BOTTOM
END-IF
END-FOR
RESET #R (*)
#J
FOR #I = 1 #C /* left-to-right
ASSIGN #J = #C - #I + 1
ASSIGN #R (#J) = #T (#I)
END-FOR
WRITE ' After:' #REC / /* debug
/* WRITE WORK 2 VARIABLE #R (1:#C) /* <<<
END-WORK
END
Page 1 10/21/11 12:03:26
Before: 123 | abcd |456 | | |
After: 123| abcd|456|||
Before: 123 |abcd |456 | | | |
After: 123|abcd|456||||
Push for a Natural upgrade, Aseem. Jerome’s solution demonstrates several ways in which the language has improved, but there are many more.
thanks