View previous topic :: View next topic
|
Author |
Message |
cigarman
New User
Joined: 19 May 2007 Posts: 25 Location: Chicago
|
|
|
|
Hello folks.
I have problem to solve and would like your input please.
I have a stem with some 1000 entries. Each entry is only 8 bytes each. Its a list of applids from CL/Stupersession.
Anyway. What I want to do is create a target stem which has only unique applids and a second stem that has a count of the applid from the original.
like so.
Source stem
a.1 = TSO
a.2 = NDM
a.3 = TSO
Target stems
b.1 = TSO
b.2 = NDM
c.1 = 2 (two tsos in source stem)
c.2 = 1
any suggestions would be greatly appreciated.
thanks in advance
rp |
|
Back to top |
|
|
Aaru
Senior Member
Joined: 03 Jul 2007 Posts: 1287 Location: Chennai, India
|
|
|
|
Cigar,
Quote: |
I have a stem with some 1000 entries. |
How was this stem populated? If it was populated by reading an input file then
- Invoke DFSORT from REXX to eliminate the duplicates and to get the count.
AFAIK coding logic in REXX is difficult for this requirement. |
|
Back to top |
|
|
MBabu
Active User
Joined: 03 Aug 2008 Posts: 400 Location: Mumbai
|
|
|
|
use the facts that you can set a default stem's default value and that the tails of rexx stems do not need to be numbers. No comments in the code because figuring it out is a good exercise. For example, it uses stems to contain indices into other stems. If you use these techniques in code others will maintain, add GOOD comments though because at first glance, it is indecipherable.
Code: |
/* make some test data */
a.0 = 4000
do i = 1 to a.0
if random(100) < 30 then
a.i = 'TSO'
else
a.i = 'NDM'
end
cindex = 0
/* create the stems. b is unique values, c is counts */
knownvalues. = "not set"
knownvaluecount = 0
c. = 0
b. = 0
do i = 1 to a.0
val = a.i
if knownvalues.val == "not set" then
do
knownvaluecount = knownvaluecount + 1
knownvalues.val = knownvaluecount
b.knownvaluecount = val
b.0 = knownvaluecount
cindex = knownvaluecount
end
else
cindex = knownvalues.val
c.cindex = c.cindex + 1
end
c.0 = knownvaluecount
/* print the results */
do i=1 to b.0
say "b." || i || "=" || b.i
end
do i=1 to c.0
say "c." || i || "=" || c.i
end
do i = 1 to b.0
count = c.i
say "there are "c.i" instances of "b.i
end |
example output
Code: |
b.1=NDM
b.2=TSO
c.1=2831
c.2=1169
there are 2831 instances of NDM
there are 1169 instances of TSO |
|
|
Back to top |
|
|
Aaru
Senior Member
Joined: 03 Jul 2007 Posts: 1287 Location: Chennai, India
|
|
|
|
Mbabu,
Thanks for the code. It would be great if you can explain us the logic in few words. |
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Hello,
Please read the words above the code. . .
Quote: |
No comments in the code because figuring it out is a good exercise. |
Part of the contribution was the opportunity for some to learn, not just grab and run |
|
Back to top |
|
|
cigarman
New User
Joined: 19 May 2007 Posts: 25 Location: Chicago
|
|
|
|
Arau: Thanks for the comment. I will keep DFSORT in mind for future code.
Mbabu: This is really good stuff. I will dig into it tomorrow, but I think its exactly what I'm looking for. At a quick glance, it looks cool. I will have to go through it line by line to understand.
Thanks to both or you.
rp |
|
Back to top |
|
|
enrico-sorichetti
Superior Member
Joined: 14 Mar 2007 Posts: 10872 Location: italy
|
|
Back to top |
|
|
Aaru
Senior Member
Joined: 03 Jul 2007 Posts: 1287 Location: Chennai, India
|
|
|
|
Dick,
Quote: |
Part of the contribution was the opportunity for some to learn, not just grab and run |
Had i just wanted to Grab and run i would not have asked for an explanation . I would have executed it as such.
Anyways as per your suggestion let me take a look at it. |
|
Back to top |
|
|
cigarman
New User
Joined: 19 May 2007 Posts: 25 Location: Chicago
|
|
|
|
Mbabu:
This is perfect. Does exactly what I need. I went through it line by line. Desktop debugging shows me where the vars are going, but I still really don't know how its working. What would be a the term for what is going on here so I may look it up.
Thanks
rp |
|
Back to top |
|
|
MBabu
Active User
Joined: 03 Aug 2008 Posts: 400 Location: Mumbai
|
|
|
|
Here are comments on the relevant lines. I moved one line out of the loop for efficiency and clarity.
Code: |
cindex = 0
/* Create the stems. B is unique values, C is counts. */
/* Known values will contain a running list of values read from stem
a. We will check to see if a value is known by using the value as
a tail named v Knownvalues.Val will return the tail, Of the
corresponding stems b and c. If The string "Not set" Will be
returned and a new slot (Tail) Will be allocated
*/
knownvalues. = "not set"
/* Known value count is the index or tail number for b and c used
for the last new value found in a. */
knownvaluecount = 0
/* B is the list of values and c the number of entries for that value*/
c. = 0
b. = 0
/* Read stem values of a */
Do i = 1 to a.0
val = a.i /* We'll use the value as a tail in other stems */
/* See if the value is known already */
If knownvalues.val == "not set" Then /* Value is not known */
Do
/* Get a new index for stems b and c to hold info on value */
knownvaluecount = knownvaluecount + 1
/* Save value as next entry in knownvalues */
knownvalues.val = knownvaluecount
/* Save actual value in stem b at next index */
b.knownvaluecount = val
/* Set cindex to new index value */
cindex = knownvaluecount
End
Else /* Value is already known */
cindex = knownvalues.val
/* Update the count using the existing tale for this value cindex */
c.cindex = c.cindex + 1
End
/* Since knownvalue count is the value of the last used stem,
We can use it to set the zero tail of b and c for future
reference */
b.0 = knownvaluecount
c.0 = knownvaluecount |
|
|
Back to top |
|
|
dick scherrer
Moderator Emeritus
Joined: 23 Nov 2006 Posts: 19244 Location: Inside the Matrix
|
|
|
|
Thank you
d |
|
Back to top |
|
|
|