Variable substitutions -

Has anyone tried variable susbstitutions within a substitution?
like %variable[%index%]%

1 Like

Hi shahzafar,

The variable substitution you are trying to achieve wont work in a single step, it will perform one variable substitution (inner) at a time and the outer one would still remain.
Please let us know what are you trying to achieve .

Regards,
kiranj

Hey Kiran,
That’s exactly what I am trying to do. I am already doing it in two steps and was wondering if anyone has been able to do this in one step.

As to what I am trying to do, I am trying to extract a particular element from an array of elements. The index of the element I need is available to me in the index variable and I have to extract that element from the array and include it in an email.

I currently have something that works, just wondering if there is a way to do variable substitutions within a substitution. I have come across a lot of scenarios where this would have been useful. So, wondering if anyone has attemted and solved this problem.

Thanks,
Z

Hey All

I have attempted the scenario in which i need to dynamically address the contents of a document.

I found the following code in PSUtilities service name substituteVariables.

// Get input objects from pipeline
IDataCursor idcPipeline = pipeline.getCursor();

String strInputString = null;
if (idcPipeline.first("inputString"))
{
    strInputString = (String)idcPipeline.getValue();
}
else
{ 
    // Input string is null
    return;
}

// int intInputStringLen = strInputString.length();
String strOutputString = “”;

int intCurrentIndex = 0;
int intStartIndex = 0;
int intEndIndex = 0;

//System.out.println("pipeline = " + pipeline);
while ((intStartIndex = strInputString.indexOf(‘%’,intCurrentIndex)) != -1)
{
// If %, then ignore…don’t perform variable substitution
if ((intStartIndex != 0) &&
(strInputString.charAt(intStartIndex - 1) == ‘\’))
{
strOutputString = strOutputString + strInputString.substring(intCurrentIndex, intStartIndex - 1) + ‘%’;
intCurrentIndex = intStartIndex + 1;
continue;
}

    // Find the ending %
    intEndIndex = strInputString.indexOf('%', intStartIndex + 1);
    if (intEndIndex == -1)
    {
        break;
    }

    // Print out everything before the %
    strOutputString = strOutputString + strInputString.substring(intCurrentIndex, intStartIndex);
    // Find the string to substitute
    String strStringToSubstitute = null;
    String strVariableName = strInputString.substring(intStartIndex + 1, intEndIndex);

//System.out.println("strVariableName = " + strVariableName);

    if (strVariableName.equals(""))
    {
        // We had occurence of "%%"
        intCurrentIndex = intEndIndex + 1;
        continue;
    }

    // Perform variable substitution
    StringTokenizer tokenizedString = new StringTokenizer(strVariableName, "/", false);
    int maxTokens = tokenizedString.countTokens();
    IData currentRecord = null;
    int intTokenIndex = 1;
    while (tokenizedString.hasMoreTokens())
    {
        String strCurrentToken = tokenizedString.nextToken();
        System.out.println("strCurrentToken = " + strCurrentToken);

        IDataCursor idc = null;
        if (currentRecord == null)
        {
            // New search - look in pipeline for record/string
            idc = idcPipeline;

//System.out.println(“Searching in pipeline”);
}
else
{
//System.out.println("Searching in record = " + currentRecord);
idc = currentRecord.getCursor();
}

        if (idc.first(strCurrentToken))
        {
            Object o = idc.getValue();

//System.out.println("intTokenIndex = " + intTokenIndex);
//if (o instanceof String)
//{
//System.out.println(“String!”);
//}
if ((intTokenIndex == maxTokens) && (o instanceof String))
{
// This is the last token. Look for a string
// Variable found in pipeline
strStringToSubstitute = (String)o;
strOutputString = strOutputString + strStringToSubstitute;
intCurrentIndex = intEndIndex + 1;
//System.out.println(“String found”);
//System.out.println("new strOutputString = " + strOutputString);
}
else if ((intTokenIndex != maxTokens) && (o instanceof IData))
{
// Look for a IData (record)
currentRecord = (IData)o;
//System.out.println("Record found = " + currentRecord);
}
else
{
// Type mismatch - variable not found in pipeline
strOutputString = strOutputString + strInputString.substring(intStartIndex, intEndIndex + 1);
intCurrentIndex = intEndIndex + 1;
break; // Ignore other tokens
}
}
else
{
// Variable not found in pipeline
strOutputString = strOutputString + strInputString.substring(intStartIndex, intEndIndex + 1);
intCurrentIndex = intEndIndex + 1;
break; // Ignore other tokens
}

        intTokenIndex++;
    }

}

strOutputString = strOutputString + strInputString.substring(intCurrentIndex);

idcPipeline.insertAfter("outputString", strOutputString);
idcPipeline.destroy();

just pass the input string and it will replace the variables.
Hope this helps.
DO let me know if you have any questions.