Need help in a range string formation logic

Post questions here relative to DataStage Enterprise/PX Edition for such areas as Parallel job design, Parallel datasets, BuildOps, Wrappers, etc.

Moderators: chulett, rschirm, roy

Post Reply
muralisankarr
Premium Member
Premium Member
Posts: 83
Joined: Tue Oct 28, 2008 1:55 am
Location: Chennai

Need help in a range string formation logic

Post by muralisankarr »

Hi,

We need to form a range pattern to the stings we have. For example

213,001,052,219,220,221v,051,106,102,210,212,103,101,104,211,105,201,202,203,204,205,206,207,208,209,214,215,216,217,218
Should be converted to range pattern as below
001,051-052,101-106,201-221
The input string is sorted and ranges are formed on continuous set of values.

I don't want to go for transformer substringing, horizontal pivot, sort, vertical pivot and remove duplicate stages to handle this as the quantum of jobs where I have to implement this is quite high. Even container with RCP is not a very good option as the computation will be high in multiple instances. Other way I think is stored procedure stage or parallel routine. I don't have c++ skill set. So it is not possible to write any px routines.

If you have some idea to deal with it please suggest

Many Thanks
MSR
The minute you start talking about what you're going to do if you lose, you have lost
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

I have had a tough day at work doing some complex modifications and needed a break, so I wrote a quite BASIC routine to do this. If you use a BASIC transform stage and call up

MakeRangeString(InString)

Code: Select all

****************************************************************************************************************************************************************
** Make a range list out of a comma separated list of numeric values.                                                                                         **
**                                                                                                                                                            **
** Version Date       Programmer           Comments                                                                                                           **
** ======= ========== ==================== ================================================================================================================== **
** 1.0.0   2010-07-06 A. Wussing           Initial Version                                                                                                    **
**                                                                                                                                                            **
****************************************************************************************************************************************************************
   ** Declare include files and constants                                                                                                                     **
   *************************************************************************************************************************************************************
   EQUATE ProgramName  TO "MakeRange"                   ;** Job name to display in DSLog(Info/Warn/Fatal) calls.                                              **
   EQUATE Delimiter    TO ","                           ;** delimiter used in string.                                                                         **
   $COPYRIGHT "(c) 2010 -FreeWare-, no rights reserved" ;** additional Copyright notice put into object code.                                                 **
   $UNDEFINE DebugSwitch                                ;** Debugging turned on or off depending on whether $DEFINE or $UNDEFINE is set here.                 **
                                                        ;*******************************************************************************************************
   WorkString = CONVERT(Delimiter,@FM,InString) ;** make a copy of the parameter
   ************************************
   ** First step is to sort the data **
   ************************************
   Top = DCOUNT(WorkString,@FM)-1
   $IFDEF DebugSwitch CALL DSLogInfo('Entering Bubble Sort with ':Top:' Entries in dynamic sort array.',ProgramName) $ENDIF
   IF Top > 0
   THEN
      DataHasChanged = 1 ;** set to true so we enter the main loop
      LOOP WHILE DataHasChanged
         DataHasChanged = 0 ;** assume that we didn't switch any rows this loop iteration
         FOR RowNum = 1 TO Top
            IF WorkString<RowNum> > WorkString<RowNum+1>
            THEN
               $IFDEF DebugSwitch CALL DSLogInfo('Switching Entries ':i:' and ':i+1:' (Sort).',ProgramName) $ENDIF
               Temp                  = WorkString<RowNum>   ;************************************
               WorkString<RowNum>    = WorkString<RowNum+1> ;** switch the two elements around **
               WorkString<RowNum+1>  = Temp                 ;**                                **
               DataHasChanged = 1                           ;************************************
            END
         NEXT RowNum
         Top -= 1 ;** highest entry can be skipped next time around
      REPEAT
   END ;** of if-then we need to sort, i.e. if there are 2 or more rows in the data
   *************************************************************
   ** Now that we have an ordered list, create the range list **
   *************************************************************
   Ans = '' ;** empty output string
   CurrentRangeStart = WorkString<1> ;** start at the first element
   CurrentRangeEnd   = WorkString<1> ;** same as the start
   FOR i = 2 TO Top+1
      IF WorkString<i> = CurrentRangeEnd+1
      THEN
         CurrentRangeEnd += 1
      END
      ELSE
         ************************************
         ** Not adjacent, so add to output **
         ************************************
         IF CurrentRangeStart=CurrentRangeEnd THEN Ans<-1> = CurrentRangeStart
                                              ELSE Ans<-1> = CurrentRangeStart:'-':CurrentRangeEnd
         CurrentRangeStart = WorkString<i> ;** start at current element
         CurrentRangeEnd   = WorkString<i> ;** same as the start
      END ;** of if-then-else new element is adjacent to old
   NEXT i
   Ans = CONVERT(@FM,Delimiter,Ans) ;** reconvert back to original format.
chulett
Charter Member
Charter Member
Posts: 43085
Joined: Tue Nov 12, 2002 4:34 pm
Location: Denver, CO

Post by chulett »

So... this is how you take a break, eh? :wink:
-craig

"You can never have too many knives" -- Logan Nine Fingers
muralisankarr
Premium Member
Premium Member
Posts: 83
Joined: Tue Oct 28, 2008 1:55 am
Location: Chennai

Post by muralisankarr »

Many thanks AndW. I have started analyzing the code as I have issues with string like one discreet chain. For example 1,2,3 to 1-3 or 1,2 to 1-2 or 1 to 1. I will post the modified code here. Many thanks for your time and baseline code. I can't imagine being employed in a ds job with out dsxchange :D
The minute you start talking about what you're going to do if you lose, you have lost
ray.wurlod
Participant
Posts: 54607
Joined: Wed Oct 23, 2002 10:52 pm
Location: Sydney, Australia
Contact:

Post by ray.wurlod »

chulett wrote:So... this is how you take a break, eh? :wink:
You orta see how he navigates his boat!!! :wink:
IBM Software Services Group
Any contribution to this forum is my own opinion and does not necessarily reflect any position that IBM may hold.
ArndW
Participant
Posts: 16318
Joined: Tue Nov 16, 2004 9:08 am
Location: Germany
Contact:

Post by ArndW »

I should add that I did a quickie bubble sort last week and just used that code, but I was interested in how I would solve the range logic and happened to have a designer open.

I'm not sure what you mean by "have issues with string like one discreet chain". The code uses the variables "CurrentRangeStart" and "CurrentRangeEnd"; when they are identical it means the range is just one, if they are not then it is a x-y range.
Post Reply