Search code examples
datastage

How can I loop thorough a record one character at a time?


How can I loop thorough a record one character at a time? I want to interrogate records in a ASCII file one character at a time looking for and replacing non-printable characters. I tried using the Loop Condition with no luck. Thanks in advance for any help.


Solution

  • Yes, this is possible in a DataStage Transformer.

    The "Loop Condition" creates a new output row per iteration. It operates on each row it receives. You might want to add a constraint to your output link which is only true when your loop iterations are finished (for that row).

    A little pseudo-code to replace every 'ä' with a '?' by looping through char-by-char:

    // Input Link "DSLink2" 
    // provides a column named "text"
    
    
    // Stage Variables:
    
    NVarCHar(20) svLine := DSLink2.text
    NChar(1)     svReplacementChar := "?"
    
    
    // Loop Variables:
    
    NChar(1)     lvCharToTest
    Bit          lvCharOK := 0
    NVarCHar(20) lvNewLine := ''
    Integer      i := 0
    
    
    // Loop Condition:
    
    loop while (i < Len(svLine)) {
      i = i + 1
      lvCharToTest := svLine[i,1]
    
      // replace by proper test condition or function according to your needs:
      lvCharOK     := lvCharToTest = 'ä' 
    
      if (lvCharOK) {
        lvNewLine = lvNewLine + lvCharToTest
      }
      else {
        lvNewLine = lvNewLine + svReplacementChar
      }
    }
    
    // DSLink4_output:
    
    If (i = Len(svLine)) {
      DSLInk4_output.text := lvNewLine
    }
    

    Screenshot of a Transformer Stage showing this example