View previous topic :: View next topic
|
Author |
Message |
vnktrrd
New User
Joined: 12 Jan 2010 Posts: 34 Location: New York
|
|
|
|
Hi,
Thanks in advance for your support. Here is my requirement :
I have a file like below with first 4 characters as key.
AAAA
AAAA
BBBB
BBBB
BBBB
CCCC
AAAA
BBBB
BBBB
CCCC
CCCC
DDDD
DDDD
DDDD
I want a count of number of times the key changed. Count for above example is 6.
Please let me know how to do this in DFSORT.
Thanks. |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
vnktrrd,
If you have DFSORT, post your queries in the DFSORT forum.
One way to achieve this is, use WHEN=GROUP and KEYBEGIN, to add group IDs, then in the output write the last record using OUTFIL TRAILER1. This record will have the number of groups - 7 as per your example.
This can be tweaked to arrive at the result - 6. Good luck |
|
Back to top |
|
|
vnktrrd
New User
Joined: 12 Jan 2010 Posts: 34 Location: New York
|
|
|
|
Hi Arun,
Would you please elaborate ?
Thanks. |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Assign group-id for each group as below.
Code: |
AAAA 00000001
AAAA 00000001
BBBB 00000002
BBBB 00000002
BBBB 00000002
CCCC 00000003
AAAA 00000004
BBBB 00000005
BBBB 00000005
CCCC 00000006
CCCC 00000006
DDDD 00000007
DDDD 00000007
DDDD 00000007
|
Subtract 1 from each group-id and write the last record to output.
Or assign a sequence number and restart when the key changes as below.
Code: |
AAAA 00000001
AAAA 00000002
BBBB 00000001
BBBB 00000002
BBBB 00000003
CCCC 00000001
AAAA 00000001
BBBB 00000001
BBBB 00000002
CCCC 00000001
CCCC 00000002
DDDD 00000001
DDDD 00000002
DDDD 00000003 |
Write the sum/count of 1's - 1 to output. |
|
Back to top |
|
|
sergeyken
Senior Member
Joined: 29 Apr 2008 Posts: 2141 Location: USA
|
|
|
|
The TS wanna get the ready-to-use code, to copy-paste it into his project.
The task is equivalent to an exercise for a first year student.
Nowadays too many people don't want to read and understand the manuals. I'm not sure if they can read, at all? |
|
Back to top |
|
|
expat
Global Moderator
Joined: 14 Mar 2007 Posts: 8796 Location: Welsh Wales
|
|
|
|
To be honest, I'm a bit lost by the whole question.
There is no real explanation as to what is required, and there is no example of the expected output.
Just some advice, please learn quickly to explain what you need, in detail, giving examples of input / output where possible. You might understand exactly what it is that you need, but as I have said above. It took me two or three reads to finally work it out.
You also need to show what YOU have tried yourself, and the results given and the results expected. From your example above what did you expect out of the exercise.
You will almost certainly always be asked to provide what you have tried yourself.
Another important thing to remember is that there are such things called code tags - learn about them, and use them !!! After 33 posts I'm sure that you must have used them before, if at least not seen them used.
As sergeyken intimates, it seems that you want the ready to run code rather than make any effort yourself. Unfortunately a lot of us supposedly experts have a pretty accurate idea of when this is the case.
So, just some friendly advice >>> keep us happy, and ultimately ....... you happy. |
|
Back to top |
|
|
vnktrrd
New User
Joined: 12 Jan 2010 Posts: 34 Location: New York
|
|
|
|
Hi All,
So many quick conclusions by the time I respond. Happy to see someone out there to help.
The output I expect out of the exercise is 6 which I gave in the first post and Arun seems to have understood it correct and have responded with what options of the Sort to use. Since I have very less experience working with the DFSORT, I requested him to elaborate the solution and not to post the code.
Finally, I understood what I have to do and will post the code as soon as I complete. Thanks again for all your support. |
|
Back to top |
|
|
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
I tried to build Arun's first solution but I can't subtract 1 from last record.
Here is the code:
Code: |
OPTION COPY
INREC IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,4),PUSH=(6:ID=3))
OUTFIL REMOVECC,NODETAIL,
TRAILER1=(6,3) |
output is:
Does trailer not allow subtraction.
I get syntax error.
Code: |
TRAILER1=(6,3,SUB,+1)
£ |
|
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Quote: |
subtract 1 from last record |
I mentioned about subtracting 1 from each record and then write only the last one. Or you could try the other approach if that is easier.
SUB in TRAILER1 stands for subtotal and not subtraction, refer to the manual for more details on that. |
|
Back to top |
|
|
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
Thanks Arun. I have upgraded code to subtract 1 from each record.
Code: |
OPTION COPY
INREC IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,4),PUSH=(6:ID=3)),
IFTHEN=(WHEN=INIT,OVERLAY=(6,3,ZD,SUB,+1))
OUTFIL REMOVECC,NODETAIL,
TRAILER1=(6,3) |
It's not giving expected result because of OVERLAY line.
WHEN=INIT OVERLAY produces:
Code: |
---+----1----+-
***************
0
0
1
1
1
2
3
4
4
5
5
6
6
6 |
I do not understand why is subtracted value shifting to 16th position, and also why is key part is cleared down. I understand OVERLAY only edits targeted column not entire record. Thanks. |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
zh_lad wrote: |
I do not understand why is subtracted value shifting to 16th position, and also why is key part is cleared down. I understand OVERLAY only edits targeted column not entire record |
Your input field is just 4 bytes, but the result from arithmetic operation, in this case is 15 bytes with leading spaces by default, unless you use an EDIT. So the result is consistent with what you have provided in your control statements.
Please go through the DFSORT application programming guide for more details.
DFSORT Application Programming Guide wrote: |
An arithmetic expression produces a signed, 31-digit zoned decimal (ZD) result to be edited as specified.
If an Mn, EDIT, or EDxy parameter is not specified, the result is edited using the M0 edit mask.
The default number of digits (d) used for editing is 15 if every term in the expression is one of the following:
v a 1-4 byte BI or FI field
v a 1-8 byte PD field
v a 1-15 byte ZD, FS, CSF, UFF or SFF field
v a decimal constant with 1-15 significant digits. |
|
|
Back to top |
|
|
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
Thanks Arun. I have changed my sort statement and used mask. This produces expected result.
Code: |
INREC IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,4),PUSH=(6:ID=3))
SORT FIELDS=(6,3,ZD,D)
OUTFIL ENDREC=1,BUILD=(6,3,ZD,SUB,+1,M10,LENGTH=3) |
Any suggestion, welcome. Thanks |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
Code: |
SORT FIELDS=(6,3,ZD,D) |
Sorting is not recommended when it is really not needed. |
|
Back to top |
|
|
zh_lad
Active User
Joined: 06 Jun 2009 Posts: 115 Location: UK
|
|
|
|
Fine. I have gone back to TRAILER1 option, added Mask and it work as expected.
Code: |
OPTION COPY
INREC IFTHEN=(WHEN=GROUP,KEYBEGIN=(1,4),PUSH=(6:ID=3)),
IFTHEN=(WHEN=INIT,BUILD=(1,5,6,3,ZD,SUB,+1,M10,LENGTH=3))
OUTFIL REMOVECC,NODETAIL,
TRAILER1=(6,3) |
only doubt I have on order of IFTHEN statements. I thought WHEN=INIT won't give desired result (subtraction) as it will take precedence over GROUP.
Here is the text from SORT PDF:
IFTHEN clauses are processed in the following order:
- WHEN=INIT clauses and WHEN=GROUP clauses
- WHEN=(logexp) clauses and WHEN=ANY clauses
- WHEN=NONE clauses |
|
Back to top |
|
|
Arun Raj
Moderator
Joined: 17 Oct 2006 Posts: 2481 Location: @my desk
|
|
|
|
zh_lad,
Thanks for posting.
Quote: |
I thought WHEN=INIT won't give desired result (subtraction) as it will take precedence over GROUP |
As you see, they are at the same level of precedence and you can have combinations of WHEN=GROUP and WHEN=INIT in any order. |
|
Back to top |
|
|
|